home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / lib / tcl / Tcl.man < prev    next >
Encoding:
Text File  |  1992-08-07  |  116.6 KB  |  2,982 lines

  1. '\"
  2. '\" Copyright 1989-1992 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. '\" $Header: /user6/ouster/tcl/man/RCS/Tcl.man,v 1.99 92/07/13 21:25:26 ouster Exp $ SPRITE (Berkeley)
  11. '
  12. .\" The definitions below are for supplemental macros used in Sprite
  13. .\" manual entries.
  14. .\"
  15. .\" .HS name section [date [version]]
  16. .\"    Replacement for .TH in other man pages.  See below for valid
  17. .\"    section names.
  18. .\"
  19. .\" .AP type name in/out [indent]
  20. .\"    Start paragraph describing an argument to a library procedure.
  21. .\"    type is type of argument (int, etc.), in/out is either "in", "out",
  22. .\"    or "in/out" to describe whether procedure reads or modifies arg,
  23. .\"    and indent is equivalent to second arg of .IP (shouldn't ever be
  24. .\"    needed;  use .AS below instead)
  25. .\"
  26. .\" .AS [type [name]]
  27. .\"    Give maximum sizes of arguments for setting tab stops.  Type and
  28. .\"    name are examples of largest possible arguments that will be passed
  29. .\"    to .AP later.  If args are omitted, default tab stops are used.
  30. .\"
  31. .\" .BS
  32. .\"    Start box enclosure.  From here until next .BE, everything will be
  33. .\"    enclosed in one large box.
  34. .\"
  35. .\" .BE
  36. .\"    End of box enclosure.
  37. .\"
  38. .\" .VS
  39. .\"    Begin vertical sidebar, for use in marking newly-changed parts
  40. .\"    of man pages.
  41. .\"
  42. .\" .VE
  43. .\"    End of vertical sidebar.
  44. .\"
  45. .\" .DS
  46. .\"    Begin an indented unfilled display.
  47. .\"
  48. .\" .DE
  49. .\"    End of indented unfilled display.
  50. .\"
  51. '\"    # Heading for Sprite man pages
  52. .de HS
  53. .if '\\$2'cmds'       .TH \\$1 1 \\$3 \\$4
  54. .if '\\$2'lib'        .TH \\$1 3 \\$3 \\$4
  55. .if '\\$2'tcl'        .TH \\$1 3 \\$3 \\$4
  56. .if '\\$2'tk'         .TH \\$1 3 \\$3 \\$4
  57. .if t .wh -1.3i ^B
  58. .nr ^l \\n(.l
  59. .ad b
  60. ..
  61. '\"    # Start an argument description
  62. .de AP
  63. .ie !"\\$4"" .TP \\$4
  64. .el \{\
  65. .   ie !"\\$2"" .TP \\n()Cu
  66. .   el          .TP 15
  67. .\}
  68. .ie !"\\$3"" \{\
  69. .ta \\n()Au \\n()Bu
  70. \&\\$1    \\fI\\$2\\fP    (\\$3)
  71. .\".b
  72. .\}
  73. .el \{\
  74. .br
  75. .ie !"\\$2"" \{\
  76. \&\\$1    \\fI\\$2\\fP
  77. .\}
  78. .el \{\
  79. \&\\fI\\$1\\fP
  80. .\}
  81. .\}
  82. ..
  83. '\"    # define tabbing values for .AP
  84. .de AS
  85. .nr )A 10n
  86. .if !"\\$1"" .nr )A \\w'\\$1'u+3n
  87. .nr )B \\n()Au+15n
  88. .\"
  89. .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
  90. .nr )C \\n()Bu+\\w'(in/out)'u+2n
  91. ..
  92. '\"    # BS - start boxed text
  93. '\"    # ^y = starting y location
  94. '\"    # ^b = 1
  95. .de BS
  96. .br
  97. .mk ^y
  98. .nr ^b 1u
  99. .if n .nf
  100. .if n .ti 0
  101. .if n \l'\\n(.lu\(ul'
  102. .if n .fi
  103. ..
  104. '\"    # BE - end boxed text (draw box now)
  105. .de BE
  106. .nf
  107. .ti 0
  108. .mk ^t
  109. .ie n \l'\\n(^lu\(ul'
  110. .el \{\
  111. .\"    Draw four-sided box normally, but don't draw top of
  112. .\"    box if the box started on an earlier page.
  113. .ie !\\n(^b-1 \{\
  114. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  115. .\}
  116. .el \}\
  117. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  118. .\}
  119. .\}
  120. .fi
  121. .br
  122. .nr ^b 0
  123. ..
  124. '\"    # VS - start vertical sidebar
  125. '\"    # ^Y = starting y location
  126. '\"    # ^v = 1 (for troff;  for nroff this doesn't matter)
  127. .de VS
  128. .mk ^Y
  129. .ie n 'mc \s12\(br\s0
  130. .el .nr ^v 1u
  131. ..
  132. '\"    # VE - end of vertical sidebar
  133. .de VE
  134. .ie n 'mc
  135. .el \{\
  136. .ev 2
  137. .nf
  138. .ti 0
  139. .mk ^t
  140. \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
  141. .sp -1
  142. .fi
  143. .ev
  144. .\}
  145. .nr ^v 0
  146. ..
  147. '\"    # Special macro to handle page bottom:  finish off current
  148. '\"    # box/sidebar if in box/sidebar mode, then invoked standard
  149. '\"    # page bottom macro.
  150. .de ^B
  151. .ev 2
  152. 'ti 0
  153. 'nf
  154. .mk ^t
  155. .if \\n(^b \{\
  156. .\"    Draw three-sided box if this is the box's first page,
  157. .\"    draw two sides but no top otherwise.
  158. .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  159. .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  160. .\}
  161. .if \\n(^v \{\
  162. .nr ^x \\n(^tu+1v-\\n(^Yu
  163. \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
  164. .\}
  165. .bp
  166. 'fi
  167. .ev
  168. .if \\n(^b \{\
  169. .mk ^y
  170. .nr ^b 2
  171. .\}
  172. .if \\n(^v \{\
  173. .mk ^Y
  174. .\}
  175. ..
  176. '\"    # DS - begin display
  177. .de DS
  178. .RS
  179. .nf
  180. .sp
  181. ..
  182. '\"    # DE - end display
  183. .de DE
  184. .fi
  185. .RE
  186. .sp .5
  187. ..
  188. .de UL
  189. \\$1\l'|0\(ul'\\$2
  190. ..
  191. .HS Tcl tcl
  192. .BS
  193. .SH NAME
  194. Tcl \- overview of tool command language facilities
  195. .BE
  196.  
  197. .SH INTRODUCTION
  198. .PP
  199. Tcl stands for ``tool command language'' and is pronounced ``tickle.''
  200. It is actually two things:
  201. a language and a library.
  202. First, Tcl is a simple textual language,
  203. intended primarily for issuing commands to interactive programs such
  204. as text editors, debuggers, illustrators, and shells.  It has
  205. a simple syntax and is also programmable, so
  206. Tcl users can write command procedures to provide more powerful
  207. commands than those in the built-in set.
  208. .PP
  209. Second, Tcl is a library package that can be embedded in application
  210. programs.  The Tcl library consists of a parser for the Tcl
  211. language, routines to implement the Tcl built-in commands, and
  212. procedures that allow each application to extend Tcl with additional
  213. commands specific to that application.  The application program
  214. generates Tcl commands and passes them to the Tcl parser for
  215. execution.  Commands may be generated
  216. by reading characters from an input
  217. source, or by associating command strings with elements of the
  218. application's user interface, such as menu entries, buttons, or
  219. keystrokes.
  220. When the Tcl library receives commands it parses them
  221. into component fields and executes built-in commands directly.
  222. For commands implemented by the
  223. application, Tcl calls back to the application to execute the
  224. commands.  In many cases commands will invoke recursive invocations
  225. of the Tcl interpreter by passing in additional strings to execute
  226. (procedures, looping commands, and conditional commands all work
  227. in this way).
  228. .PP
  229. An application program gains three advantages by using Tcl for
  230. its command language.  First, Tcl provides a standard syntax:  once
  231. users know Tcl, they will be able to issue commands easily
  232. to any Tcl-based application.  Second, Tcl provides programmability.
  233. All a Tcl application needs to do is to implement a few
  234. application-specific low-level commands.  Tcl provides many utility
  235. commands plus a general programming interface for building up
  236. complex command procedures.  By using Tcl, applications need not
  237. re-implement these features.  Third, Tcl can be used as
  238. .VS
  239. a common language for communicating between applications.
  240. Inter-application communication is not built into the Tcl core
  241. described here, but various add-on libraries, such as the Tk toolkit,
  242. allow applications to issue commands to each other.
  243. This makes it possible for applications to work together in much
  244. more powerful ways than was previously possible.
  245. .VE
  246. .PP
  247. This manual page focuses primarily on the Tcl language.  It describes
  248. the language syntax and the built-in commands that will be available in
  249. any application based on Tcl.  The individual library
  250. procedures are described in more detail in separate manual pages, one
  251. per procedure.
  252.  
  253. .SH "INTERPRETERS"
  254. .PP
  255. The central data structure in Tcl is an interpreter (C type
  256. ``Tcl_Interp'').  An interpreter consists of a set of command
  257. bindings, a set of variable values, and a few other miscellaneous
  258. pieces of state.  Each Tcl command is interpreted in the context
  259. of a particular interpreter.
  260. Some Tcl-based applications will maintain
  261. multiple interpreters simultaneously, each associated with a
  262. different widget or portion of the application.
  263. Interpreters are relatively lightweight structures.  They can
  264. be created and deleted quickly, so application programmers should feel free to
  265. use multiple interpreters if that simplifies the application.
  266. Eventually Tcl will provide a mechanism for sending Tcl commands
  267. and results back and forth between interpreters, even if the
  268. interpreters are managed by different processes.
  269.  
  270. .SH "DATA TYPES"
  271. .PP
  272. Tcl supports only one type of data:  strings.  All commands,
  273. all arguments to commands, all command results, and all variable values
  274. are strings.
  275. Where commands require numeric arguments or return numeric results,
  276. the arguments and results are passed as strings.
  277. Many commands expect their string arguments to have certain formats,
  278. but this interpretation is
  279. up to the individual commands.  For example, arguments often contain
  280. Tcl command strings, which may get executed as part of the commands.
  281. The easiest way to understand the Tcl interpreter is to remember that
  282. everything is just an operation on a string.  In many cases Tcl constructs
  283. will look similar to more structured constructs from other languages.
  284. However, the Tcl constructs
  285. are not structured at all; they are just strings of characters, and this
  286. gives them a different behavior than the structures they may look like.
  287. .PP
  288. Although the exact interpretation of a Tcl string depends on who is
  289. doing the interpretation, there are three common forms that strings
  290. take:  commands, expressions, and lists.  The major sections below
  291. discuss these three forms in more detail.
  292.  
  293. .SH "BASIC COMMAND SYNTAX"
  294. .PP
  295. The Tcl language has syntactic similarities to both the Unix shells
  296. and Lisp.  However, the interpretation of commands is different
  297. in Tcl than in either of those other two systems.
  298. A Tcl command string consists of one or more commands separated
  299. by newline characters or semi-colons.
  300. Each command consists of a collection of fields separated by
  301. white space (spaces or tabs).
  302. The first field must be the name of a command, and the
  303. additional fields, if any, are arguments that will be passed to
  304. that command.  For example, the command
  305. .DS
  306. \fBset a 22\fR
  307. .DE
  308. has three fields:  the first, \fBset\fR, is the name of a Tcl command, and
  309. the last two, \fBa\fR and \fB22\fR, will be passed as arguments to
  310. the \fBset\fR command.  The command name may refer either to a built-in
  311. Tcl command, an application-specific command bound in with the library
  312. procedure \fBTcl_CreateCommand\fR, or a command procedure defined with the
  313. \fBproc\fR built-in command.
  314. Arguments are passed literally as
  315. text strings.  Individual commands may interpret those strings in any
  316. fashion they wish.  The \fBset\fR command, for example, will treat its
  317. first argument as the name of a variable and its second argument as a
  318. string value to assign to that variable.  For other commands arguments
  319. may be interpreted as integers, lists, file names, or Tcl commands.
  320. .PP
  321. .VS
  322. Command names should normally be typed completely (e.g. no abbreviations).
  323. However, if the Tcl interpreter cannot locate a command it invokes a
  324. special command named \fBunknown\fR which attempts to find or create
  325. the command.
  326. For example, at many sites \fBunknown\fR will search
  327. through library directories for the desired command and create it
  328. as a Tcl procedure if it is found.
  329. The \fBunknown\fR command often provides automatic completion of
  330. abbreviated commands, but usually only for commands that were typed
  331. interactively.
  332. It's probably a bad idea to use abbreviations in command scripts
  333. and other forms that will be re-used over time:  changes
  334. to the command set may cause abbreviations to become ambiguous,
  335. resulting in scripts that no longer work.
  336. .VE
  337.  
  338. .SH "COMMENTS"
  339. .PP
  340. If the first non-blank character in a command is \fB#\fR, then everything
  341. from the \fB#\fR up through the next newline character is treated as
  342. a comment and ignored.  When comments are embedded inside nested
  343. commands (e.g. fields enclosed in braces) they must have properly-matched
  344. braces (this is necessary because when Tcl parses the top-level command
  345. it doesn't yet know that the nested field will be used as a command so
  346. it cannot process the nested comment character as a comment).
  347.  
  348. .SH "GROUPING ARGUMENTS WITH DOUBLE-QUOTES"
  349. .PP
  350. Normally each argument field ends at the next white space, but
  351. double-quotes may be used to create arguments with embedded
  352. space.  If an argument
  353. field begins with a double-quote, then the argument isn't
  354. terminated by white space (including newlines) or a semi-colon
  355. (see below for information on semi-colons); instead it ends at the next
  356. double-quote character.  The double-quotes are not included
  357. in the resulting argument.  For example, the
  358. command
  359. .DS
  360. \fBset a "This is a single argument"\fR
  361. .DE
  362. will pass two arguments to \fBset\fR:  \fBa\fR and
  363. \fBThis is a single argument\fR.  Within double-quotes, command
  364. substitutions, variable substitutions, and backslash substitutions
  365. still occur, as described below.  If the first character of a
  366. command field is not a quote, then quotes receive no special
  367. interpretation in the parsing of that field.
  368.  
  369. .SH "GROUPING ARGUMENTS WITH BRACES"
  370. .PP
  371. Curly braces may also be used for grouping arguments.  They are
  372. similar to quotes except for two differences.  First, they nest;
  373. this makes them easier to use for complicated arguments like nested Tcl
  374. command strings.  Second, the substitutions described below for
  375. commands, variables, and backslashes do \fInot\fR occur in arguments
  376. enclosed in braces, so braces can be used to prevent substitutions
  377. where they are undesirable.
  378. If an argument field
  379. begins with a left brace, then the argument ends at the matching
  380. right brace.  Tcl will strip off the outermost layer of braces
  381. and pass the information between the braces to the command without
  382. any further modification.  For example, in the command
  383. .DS
  384. \fBset a {xyz a {b c d}}\fR
  385. .DE
  386. the \fBset\fR command will receive two arguments: \fBa\fR
  387. and \fBxyz a {b c d}\fR.
  388. .PP
  389. When braces or quotes are in effect, the matching brace
  390. or quote need not be on
  391. the same line as the starting quote or brace; in this case
  392. the newline will be
  393. included in the argument field along with any other characters up to the
  394. matching brace or quote.  For example, the \fBeval\fR command
  395. takes one
  396. argument, which is a command string; \fBeval\fR invokes the Tcl
  397. interpreter to execute the command string.  The command
  398. .DS
  399. \fBeval {
  400.     set a 22
  401.     set b 33
  402. }\fR
  403. .DE
  404. will assign the value \fB22\fR to \fBa\fR and \fB33\fR to \fBb\fR.
  405. .PP
  406. If the first character of a command field is not a left
  407. brace, then neither left nor right
  408. braces in the field will be treated specially (except as part of
  409. variable substitution; see below).
  410.  
  411. .SH "COMMAND SUBSTITUTION WITH BRACKETS"
  412. .PP
  413. If an open bracket occurs in a field of a command, then
  414. command substitution occurs (except for fields enclosed in
  415. braces).  All of the text up to the matching
  416. close bracket is treated as a Tcl command and executed immediately.
  417. Then the result of that command is substituted for the bracketed
  418. text.  For example, consider the command
  419. .DS
  420. \fBset a [set b]\fR
  421. .DE
  422. When the \fBset\fR command has only a single argument, it is the
  423. name of a variable and \fBset\fR returns the contents of that
  424. variable.  In this case, if variable \fBb\fR has the value \fBfoo\fR,
  425. then the command above is equivalent to the command
  426. .DS
  427. \fBset a foo\fR
  428. .DE
  429. Brackets can be used in more complex ways.  For example, if the
  430. variable \fBb\fR has the value \fBfoo\fR and the variable \fBc\fR
  431. has the value \fBgorp\fR, then the command
  432. .DS
  433. \fBset a xyz[set b].[set c]\fR
  434. .DE
  435. is equivalent to the command
  436. .DS
  437. \fBset a xyzfoo.gorp\fR
  438. .DE
  439. .VS
  440. A bracketed command may contain multiple commands separated by
  441. newlines or semi-colons in the usual fashion.
  442. In this case the value of the last command is used for substitution.
  443. For example, the command
  444. .DS
  445. \fBset a x[set b 22
  446. expr $b+2]x\fR
  447. .DE
  448. is equivalent to the command
  449. .DS
  450. \fBset a x24x\fR
  451. .DE
  452. .VE
  453. If a field is enclosed in braces then the brackets and the characters
  454. between them are not interpreted specially; they are passed through
  455. to the argument verbatim.
  456.  
  457. .SH "VARIABLE SUBSTITUTION WITH $"
  458. .PP
  459. The dollar sign (\fB$\fR) may be used as a special shorthand form
  460. for substituting variable values.
  461. If \fB$\fR appears in an argument that isn't enclosed in braces
  462. then variable substitution will occur.  The characters after
  463. the \fB$\fR, up to the first character that isn't a number, letter, or
  464. underscore, are taken as a variable name and the string value of that
  465. variable is substituted for the name.
  466. .VS
  467. For example, if variable \fBfoo\fR
  468. has the value \fBtest\fR, then the command
  469. .DS C
  470. \fBset a $foo.c\fR
  471. .DE
  472. is equivalent to the command
  473. .DS C
  474. \fBset a test.c\fR
  475. .DE
  476. .PP
  477. There are two special forms for variable substitution.
  478. If the next character after the name of the variable is an
  479. open parenthesis, then the variable is assumed to be an array
  480. name, and all of the characters between the open parenthesis
  481. and the next close parenthesis are taken as an index into the array.
  482. Command substitutions and variable substitutions are
  483. performed on the information between the parentheses before it is
  484. used as an index.
  485. For example, if the variable \fBx\fR is an array with one element
  486. named \fBfirst\fR and value \fB87\fR and another element named
  487. \fB14\fR and value \fBmore\fR, then the command
  488. .DS C
  489. \fBset a xyz$x(first)zyx
  490. .DE
  491. is equivalent to the command
  492. .DS C
  493. \fBset a xyz87zyx\fR
  494. .DE
  495. If the variable \fBindex\fR has the value \fB14\fR, then the command
  496. .DS C
  497. \fBset a xyz$x($index)zyx
  498. .DE
  499. is equivalent to the command
  500. .DS C
  501. \fBset a xyzmorezyx
  502. .DE
  503. For more information on arrays, see VARIABLES AND ARRAYS below.
  504. .PP
  505. The second special form for variables occurs when
  506. the dollar sign is followed by an open curly brace.
  507. In this case the variable name consists of all the characters
  508. up to the next curly brace.
  509. Array references are not possible in this form:  the name
  510. between braces is assumed to refer to a scalar variable.
  511. For example, if variable \fBfoo\fR has the value \fBtest\fR,
  512. then the command
  513. .DS C
  514. \fBset a abc${foo}bar\fR
  515. .DE
  516. is equivalent to the command
  517. .DS C
  518. \fBset a abctestbar\fR
  519. .DE
  520. .VE
  521. Variable substitution does not occur in arguments that are enclosed
  522. in braces:  the
  523. dollar sign and variable name are passed through to the argument verbatim.
  524. .PP
  525. The dollar sign abbreviation is simply a shorthand form.  \fB$a\fR is
  526. completely equivalent to \fB[set a]\fR; it is provided as a convenience
  527. to reduce typing.
  528.  
  529. .SH "SEPARATING COMMANDS WITH SEMI-COLONS"
  530. .PP
  531. Normally, each command occupies one line (the command is terminated by
  532. a newline character).  However, semi-colon (``;'') is treated
  533. as a command separator character; multiple commands may be placed
  534. on one line by separating them with a semi-colon.  Semi-colons are
  535. not treated as command separators if they appear within curly braces
  536. or double-quotes.
  537.  
  538. .SH "BACKSLASH SUBSTITUTION"
  539. .PP
  540. Backslashes may be used to insert non-printing characters into
  541. command fields and also to insert special characters like
  542. braces and brackets into fields
  543. without them being interpreted specially as described above.
  544. The backslash sequences understood by the Tcl interpreter are
  545. listed below.  In each case, the backslash
  546. sequence is replaced by the given character:
  547. .TP 20
  548. \fB\eb\fR
  549. Backspace (0x8).
  550. .TP 20
  551. \fB\ef\fR
  552. Form feed (0xc).
  553. .TP 20
  554. \fB\en\fR
  555. Newline (0xa).
  556. .TP 20
  557. \fB\er\fR
  558. Carriage-return (0xd).
  559. .TP 20
  560. \fB\et\fR
  561. Tab (0x9).
  562. .TP 20
  563. \fB\ev\fR
  564. Vertical tab (0xb).
  565. .TP 20
  566. \fB\e{\fR
  567. Left brace (``{'').
  568. .TP 20
  569. \fB\e}\fR
  570. Right brace (``}'').
  571. .TP 20
  572. \fB\e[\fR
  573. Open bracket (``['').
  574. .TP 20
  575. \fB\e]\fR
  576. Close bracket (``]'').
  577. .TP 20
  578. \fB\e$\fR
  579. Dollar sign (``$'').
  580. .TP 20
  581. \fB\e<space>\fR
  582. Space (`` ''): doesn't terminate argument.
  583. .br
  584. .TP 20
  585. \fB\e;\fR
  586. Semi-colon: doesn't terminate command.
  587. .TP 20
  588. \fB\e"\fR
  589. Double-quote.
  590. .TP 20
  591. \fB\e<newline>\fR
  592. Nothing:  this joins two lines together
  593. into a single line.  This backslash feature is unique in that
  594. it will be applied even when the sequence occurs within braces.
  595. .TP 20
  596. \fB\e\e\fR
  597. Backslash (``\e'').
  598. .TP 20
  599. \fB\e\fIddd\fR
  600. The digits \fIddd\fR (one, two, or three of them) give the octal value of
  601. the character.  Null characters may not be embedded in command fields;
  602. if \fIddd\fR is zero then the backslash sequence is ignored (i.e. it
  603. maps to an empty string).
  604. .PP
  605. For example, in the command
  606. .DS
  607. \fBset a \e{x\e[\e\0yz\e141\fR
  608. .DE
  609. the second argument to \fBset\fR will be ``\fB{x[\0yza\fR''.
  610. .PP
  611. If a backslash is followed by something other than one of the options
  612. described above, then the backslash is transmitted to the argument
  613. field without any special processing, and the Tcl scanner continues
  614. normal processing with the next character.  For example, in the
  615. command
  616. .DS
  617. \fBset \e*a \e\e\e{foo\fR
  618. .DE
  619. The first argument to \fBset\fR will be \fB\e*a\fR and the second
  620. argument will be \fB\e{foo\fR.
  621. .PP
  622. If an argument is enclosed in braces, then backslash sequences inside
  623. the argument are parsed but no substitution occurs (except for
  624. backslash-newline):  the backslash
  625. sequence is passed through to the argument as is, without making
  626. any special interpretation of the characters in the backslash sequence.
  627. In particular, backslashed braces are not counted in locating the
  628. matching right brace that terminates the argument.
  629. For example, in the
  630. command
  631. .DS
  632. \fBset a {\e{abc}\fR
  633. .DE
  634. the second argument to \fBset\fR will be \fB\e{abc\fR.
  635. .PP
  636. This backslash mechanism is not sufficient to generate absolutely
  637. any argument structure; it only covers the
  638. most common cases.  To produce particularly complicated arguments
  639. it is probably easiest to use the \fBformat\fR command along with
  640. command substitution.
  641.  
  642. .SH "COMMAND SUMMARY"
  643. .IP [1]
  644. A command is just a string.
  645. .IP [2]
  646. Within a string commands are separated by newlines or semi-colons
  647. (unless the newline or semi-colon is within braces or brackets
  648. or is backslashed).
  649. .IP [3]
  650. A command consists of fields.  The first field is the name of the command.
  651. The other fields are strings that are passed to that command as arguments.
  652. .IP [4]
  653. Fields are normally separated by white space.
  654. .IP [5]
  655. Double-quotes allow white space and semi-colons to appear within
  656. a single argument.
  657. Command substitution, variable substitution, and backslash substitution
  658. still occur inside quotes.
  659. .IP [6]
  660. Braces defer interpretation of special characters.
  661. If a field begins with a left brace, then it consists of everything
  662. between the left brace and the matching right brace. The
  663. braces themselves are not included in the argument.
  664. No further processing is done on the information between the braces
  665. except that backslash-newline sequences are eliminated.
  666. .IP [7]
  667. If a field doesn't begin with a brace then backslash,
  668. variable, and command substitution are done on the field.  Only a
  669. single level of processing is done:  the results of one substitution
  670. are not scanned again for further substitutions or any other
  671. special treatment.  Substitution can
  672. occur on any field of a command, including the command name
  673. as well as the arguments.
  674. .IP [8]
  675. If the first non-blank character of a command is a \fB#\fR, everything
  676. from the \fB#\fR up through the next newline is treated as a comment
  677. and ignored.
  678.  
  679. .SH "EXPRESSIONS"
  680. .VS
  681. .PP
  682. The second major interpretation applied to strings in Tcl is
  683. as expressions.  Several commands, such as \fBexpr\fR, \fBfor\fR,
  684. and \fBif\fR, treat one or more of their arguments as expressions
  685. and call the Tcl expression processors (\fBTcl_ExprLong\fR,
  686. \fBTcl_ExprBoolean\fR, etc.) to evaluate them.
  687. The operators permitted in Tcl expressions are a subset of
  688. the operators permitted in C expressions, and they have the
  689. same meaning and precedence as the corresponding C operators.
  690. Expressions almost always yield numeric results
  691. (integer or floating-point values).
  692. For example, the expression
  693. .DS
  694. \fB8.2 + 6\fR
  695. .DE
  696. evaluates to 14.2.
  697. Tcl expressions differ from C expressions in the way that
  698. operands are specified, and in that Tcl expressions support
  699. non-numeric operands and string comparisons.
  700. .PP
  701. A Tcl expression consists of a combination of operands, operators,
  702. and parentheses.
  703. White space may be used between the operands and operators and
  704. parentheses; it is ignored by the expression processor.
  705. Where possible, operands are interpreted as integer values.
  706. Integer values may be specified in decimal (the normal case), in octal (if the
  707. first character of the operand is \fB0\fR), or in hexadecimal (if the first
  708. two characters of the operand are \fB0x\fR).
  709. If an operand does not have one of the integer formats given
  710. above, then it is treated as a floating-point number if that is
  711. possible.  Floating-point numbers may be specified in any of the
  712. ways accepted by an ANSI-compliant C compiler (except that the
  713. ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in
  714. most installations).  For example, all of the
  715. following are valid floating-point numbers:  2.1, 3., 6e4, 7.91e+16.
  716. If no numeric interpretation is possible, then an operand is left
  717. as a string (and only a limited set of operators may be applied to
  718. it).
  719. .PP
  720. Operands may be specified in any of the following ways:
  721. .IP [1]
  722. As an numeric value, either integer or floating-point.
  723. .IP [2]
  724. As a Tcl variable, using standard \fB$\fR notation.
  725. The variable's value will be used as the operand.
  726. .IP [3]
  727. As a string enclosed in double-quotes.
  728. The expression parser will perform backslash, variable, and
  729. command substitutions on the information between the quotes,
  730. and use the resulting value as the operand
  731. .IP [4]
  732. As a string enclosed in braces.
  733. The characters between the open brace and matching close brace
  734. will be used as the operand without any substitutions.
  735. .IP [5]
  736. As a Tcl command enclosed in brackets.
  737. The command will be executed and its result will be used as
  738. the operand.
  739. .LP
  740. Where substitutions occur above (e.g. inside quoted strings), they
  741. are performed by the expression processor.
  742. However, an additional layer of substitution may already have
  743. been performed by the command parser before the expression
  744. processor was called.
  745. As discussed below, it is usually best to enclose expressions
  746. in braces to prevent the command parser from performing substitutions
  747. on the contents.
  748. .PP
  749. For some examples of simple expressions, suppose the variable
  750. \fBa\fR has the value 3 and
  751. the variable \fBb\fR has the value 6.
  752. Then the expression on the left side of each of the lines below
  753. will evaluate to the value on the right side of the line:
  754. .DS
  755. .ta 6c
  756. \fB3.1 + $a    6.1
  757. 2 + "$a.$b"    5.6
  758. 4*[llength "6 2"]    8
  759. {word one} < "word $a"    0\fR
  760. .DE
  761. .PP
  762. The valid operators are listed below, grouped in decreasing order
  763. of precedence:
  764. .TP 20
  765. \fB\-\0\0~\0\0!\fR
  766. Unary minus, bit-wise NOT, logical NOT.  None of these operands
  767. may be applied to string operands, and bit-wise NOT may be
  768. applied only to integers.
  769. .TP 20
  770. \fB*\0\0/\0\0%\fR
  771. Multiply, divide, remainder.  None of these operands may be
  772. applied to string operands, and remainder may be applied only
  773. to integers.
  774. .TP 20
  775. \fB+\0\0\-\fR
  776. Add and subtract.  Valid for any numeric operands.
  777. .TP 20
  778. \fB<<\0\0>>\fR
  779. Left and right shift.  Valid for integer operands only.
  780. .TP 20
  781. \fB<\0\0>\0\0<=\0\0>=\fR
  782. Boolean less, greater, less than or equal, and greater than or equal.
  783. Each operator produces 1 if the condition is true, 0 otherwise.
  784. These operators may be applied to strings as well as numeric operands,
  785. in which case string comparison is used.
  786. .TP 20
  787. \fB==\0\0!=\fR
  788. Boolean equal and not equal.  Each operator produces a zero/one result.
  789. Valid for all operand types.
  790. .TP 20
  791. \fB&\fR
  792. Bit-wise AND.  Valid for integer operands only.
  793. .TP 20
  794. \fB^\fR
  795. Bit-wise exclusive OR.  Valid for integer operands only.
  796. .TP 20
  797. \fB|\fR
  798. Bit-wise OR.  Valid for integer operands only.
  799. .TP 20
  800. \fB&&\fR
  801. Logical AND.  Produces a 1 result if both operands are non-zero, 0 otherwise.
  802. Valid for numeric operands only (integers or floating-point).
  803. .TP 20
  804. \fB||\fR
  805. Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
  806. Valid for numeric operands only (integers or floating-point).
  807. .TP 20
  808. \fIx\fB?\fIy\fB:\fIz\fR
  809. If-then-else, as in C.  If \fIx\fR
  810. evaluates to non-zero, then the result is the value of \fIy\fR.
  811. Otherwise the result is the value of \fIz\fR.
  812. The \fIx\fR operand must have a numeric value.
  813. .LP
  814. See the C manual for more details on the results
  815. produced by each operator.
  816. All of the binary operators group left-to-right within the same
  817. precedence level.  For example, the expression
  818. .DS
  819. \fB4*2 < 7\fR
  820. .DE
  821. evaluates to 0.
  822. .PP
  823. The \fB&&\fP, \fB||\fP, and \fB?:\fP operators have ``lazy
  824. evaluation'', just as in C, 
  825. which means that operands are not evaluated if they are
  826. not needed to determine the outcome.  For example, in
  827. .DS
  828. \fB$v ? [a] : [b]\fR
  829. .DE
  830. only one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,
  831. depending on the value of \fB$v\fP.
  832. .PP
  833. All internal computations involving integers are done with the C type
  834. \fIlong\fP, and all internal computations involving floating-point are
  835. done with the C type \fIdouble\fP.
  836. When converting a string to floating-point, exponent overflow is
  837. detected and results in a Tcl error.
  838. For conversion to integer from string, detection of overflow depends
  839. on the behavior of some routines in the local C library, so it should
  840. be regarded as unreliable.
  841. In any case, overflow and underflow are generally not detected
  842. reliably for intermediate results.
  843. .PP
  844. Conversion among internal representations for integer, floating-point,
  845. and string operands is done automatically as needed.
  846. For arithmetic computations, integers are used until some
  847. floating-point number is introduced, after which floating-point is used.
  848. For example,
  849. .DS
  850. \fB5 / 4\fR
  851. .DE
  852. yields the result 1, while
  853. .DS
  854. \fB5 / 4.0\fR
  855. \fB5 / ( [string length "abcd"] + 0.0 )
  856. .DE
  857. both yield the result 1.25.
  858. .PP
  859. String values may be used as operands of the comparison operators,
  860. although the expression evaluator tries to do comparisons as integer
  861. or floating-point when it can.
  862. If one of the operands of a comparison is a string and the other
  863. has a numeric value, the numeric operand is converted back to
  864. a string using the C \fIsprintf\fP format specifier
  865. \fB%d\fR for integers and \fB%g\fR for floating-point values.
  866. For example, the expressions
  867. .DS
  868. \fB"0x03" > "2"\fR
  869. \fB"0y" < "0x12"\fR
  870. .DE
  871. both evaluate to 1.  The first comparison is done using integer
  872. comparison, and the second is done using string comparison after
  873. the second operand is converted to the string ``18''.
  874. .VE
  875. .PP
  876. In general it is safest to enclose an expression in braces when
  877. entering it in a command:  otherwise, if the expression contains
  878. any white space then the Tcl interpreter will split it
  879. among several arguments.  For example, the command
  880. .DS C
  881. \fBexpr $a + $b\fR
  882. .DE
  883. results in three arguments being passed to \fBexpr\fR:  \fB$a\fR,
  884. \fB+\fR, and \fB$b\fR.  In addition, if the expression isn't in braces
  885. then the Tcl interpreter will perform variable and command substitution
  886. immediately (it will happen in the command parser rather than in
  887. the expression parser).  In many cases the expression is being
  888. passed to a command that will evaluate the expression later (or
  889. even many times if, for example, the expression is to be used to
  890. decide when to exit a loop).  Usually the desired goal is to re-do
  891. the variable or command substitutions each time the expression is
  892. evaluated, rather than once and for all at the beginning.  For example,
  893. the command
  894. .DS C
  895. .ta 7c
  896. \fBfor {set i 1} $i<=10 {incr i} {...}\fR    *** WRONG ***
  897. .DE
  898. is probably intended to iterate over all values of \fBi\fR from 1 to 10.
  899. After each iteration of the body of the loop, \fBfor\fR will pass
  900. its second argument to the expression evaluator to see whether or not
  901. to continue processing.  Unfortunately, in this case the value of \fBi\fR
  902. in the second argument will be substituted once and for all when the
  903. \fBfor\fR command is parsed.  If \fBi\fR was 0 before the \fBfor\fR
  904. command was invoked then \fBfor\fR's second argument will be \fB0<=10\fR
  905. which will always evaluate to 1, even though \fBi\fR's value eventually
  906. becomes greater than 10.  In the above case the loop will never
  907. terminate.  Instead, the expression should be placed in braces:
  908. .DS C
  909. .ta 7c
  910. \fBfor {set i 1} {$i<=10} {incr i} {...}\fR    *** RIGHT ***
  911. .DE
  912. This causes the substitution of \fBi\fR's
  913. value to be delayed; it will be re-done each time the expression is
  914. evaluated, which is the desired result.
  915.  
  916. .SH LISTS
  917. .PP
  918. The third major way that strings are interpreted in Tcl is as lists.
  919. A list is just a string with a list-like structure
  920. consisting of fields separated by white space.  For example, the
  921. string
  922. .DS
  923. \fBAl Sue Anne John\fR
  924. .DE
  925. is a list with four elements or fields.
  926. Lists have the same basic structure as command strings, except
  927. that a newline character in a list is treated as a field separator
  928. just like space or tab.  Conventions for braces and quotes
  929. and backslashes are the same for lists as for commands.  For example,
  930. the string
  931. .DS
  932. \fBa b\e c {d e {f g h}}\fR
  933. .DE
  934. is a list with three elements:  \fBa\fR, \fBb c\fR, and \fBd e {f g h}\fR.
  935. Whenever an element
  936. is extracted from a list, the same rules about braces and quotes and
  937. backslashes are applied as for commands.  Thus in the example above
  938. when the third element is extracted from the list, the result is
  939. .DS
  940. \fBd e {f g h}\fR
  941. .DE
  942. (when the field was extracted, all that happened was to strip off
  943. the outermost layer of braces).  Command substitution and
  944. variable substitution are never
  945. made on a list (at least, not by the list-processing commands; the
  946. list can always be passed to the Tcl interpreter for evaluation).
  947. .PP
  948. The Tcl commands \fBconcat\fR, \fBforeach\fR, 
  949. .VS
  950. \fBlappend\fR, \fBlindex\fR, \fBlinsert\fR, \fBlist\fR, \fBllength\fR,
  951. \fBlrange\fR, \fBlreplace\fR, \fBlsearch\fR, and \fBlsort\fR allow
  952. you to build lists,
  953. .VE
  954. extract elements from them, search them, and perform other list-related
  955. functions.
  956.  
  957. .SH "REGULAR EXPRESSIONS"
  958. .VS
  959. .PP
  960. Tcl provides two commands that support string matching using
  961. \fBegrep\fR-style regular expressions: \fBregexp\fR and \fBregsub\fR.
  962. Regular expressions are implemented using Henry Spencer's package,
  963. and the description of regular expressions below is copied verbatim
  964. from his manual entry.
  965. .PP
  966. A regular expression is zero or more \fIbranches\fR, separated by ``|''.
  967. It matches anything that matches one of the branches.
  968. .PP
  969. A branch is zero or more \fIpieces\fR, concatenated.
  970. It matches a match for the first, followed by a match for the second, etc.
  971. .PP
  972. A piece is an \fIatom\fR possibly followed by ``*'', ``+'', or ``?''.
  973. An atom followed by ``*'' matches a sequence of 0 or more matches of the atom.
  974. An atom followed by ``+'' matches a sequence of 1 or more matches of the atom.
  975. An atom followed by ``?'' matches a match of the atom, or the null string.
  976. .PP
  977. An atom is a regular expression in parentheses (matching a match for the
  978. regular expression), a \fIrange\fR (see below), ``.''
  979. (matching any single character), ``^'' (matching the null string at the
  980. beginning of the input string), ``$'' (matching the null string at the
  981. end of the input string), a ``\e'' followed by a single character (matching
  982. that character), or a single character with no other significance
  983. (matching that character).
  984. .PP
  985. A \fIrange\fR is a sequence of characters enclosed in ``[]''.
  986. It normally matches any single character from the sequence.
  987. If the sequence begins with ``^'',
  988. it matches any single character \fInot\fR from the rest of the sequence.
  989. If two characters in the sequence are separated by ``\-'', this is shorthand
  990. for the full list of ASCII characters between them
  991. (e.g. ``[0-9]'' matches any decimal digit).
  992. To include a literal ``]'' in the sequence, make it the first character
  993. (following a possible ``^'').
  994. To include a literal ``\-'', make it the first or last character.
  995. .PP
  996. If a regular expression could match two different parts of a string,
  997. it will match the one which begins earliest.
  998. If both begin in the same place but match different lengths, or match
  999. the same length in different ways, life gets messier, as follows.
  1000. .PP
  1001. In general, the possibilities in a list of branches are considered in
  1002. left-to-right order, the possibilities for ``*'', ``+'', and ``?'' are
  1003. considered longest-first, nested constructs are considered from the
  1004. outermost in, and concatenated constructs are considered leftmost-first.
  1005. The match that will be chosen is the one that uses the earliest
  1006. possibility in the first choice that has to be made.
  1007. If there is more than one choice, the next will be made in the same manner
  1008. (earliest possibility) subject to the decision on the first choice.
  1009. And so forth.
  1010. .PP
  1011. For example, ``(ab|a)b*c'' could match ``abc'' in one of two ways.
  1012. The first choice is between ``ab'' and ``a''; since ``ab'' is earlier, and does
  1013. lead to a successful overall match, it is chosen.
  1014. Since the ``b'' is already spoken for,
  1015. the ``b*'' must match its last possibility\(emthe empty string\(emsince
  1016. it must respect the earlier choice.
  1017. .PP
  1018. In the particular case where no ``|''s are present and there is only one
  1019. ``*'', ``+'', or ``?'', the net effect is that the longest possible
  1020. match will be chosen.
  1021. So ``ab*'', presented with ``xabbbby'', will match ``abbbb''.
  1022. Note that if ``ab*'' is tried against ``xabyabbbz'', it
  1023. will match ``ab'' just after ``x'', due to the begins-earliest rule.
  1024. (In effect, the decision on where to start the match is the first choice
  1025. to be made, hence subsequent choices must respect it even if this leads them
  1026. to less-preferred alternatives.)
  1027. .VE
  1028.  
  1029. .SH "COMMAND RESULTS"
  1030. .PP
  1031. Each command produces two results:  a code and a string.  The
  1032. code indicates whether the command completed successfully or not,
  1033. and the string gives additional information.  The valid codes are
  1034. defined in tcl.h, and are:
  1035. .RS
  1036. .TP 20
  1037. \fBTCL_OK\fR
  1038. This is the normal return code, and indicates that the command completed
  1039. successfully.  The string gives the command's return value.
  1040. .TP 20
  1041. \fBTCL_ERROR\fR
  1042. Indicates that an error occurred; the string gives a message describing
  1043. the error.
  1044. .VS
  1045. In addition, the global variable \fBerrorInfo\fR will contain
  1046. human-readable information
  1047. describing which commands and procedures were being executed when the
  1048. error occurred, and the global variable \fBerrorCode\fR will contain
  1049. machine-readable details about the error, if they are available.
  1050. See the section BUILT-IN VARIABLES below for more information.
  1051. .VE
  1052. .VE
  1053. .TP 20
  1054. \fBTCL_RETURN\fR
  1055. Indicates that the \fBreturn\fR command has been invoked, and that the
  1056. current procedure (or top-level command or \fBsource\fR command)
  1057. should return immediately.  The
  1058. string gives the return value for the procedure or command.
  1059. .TP 20
  1060. \fBTCL_BREAK\fR
  1061. Indicates that the \fBbreak\fR command has been invoked, so the
  1062. innermost loop should abort immediately.  The string should always
  1063. be empty.
  1064. .TP 20
  1065. \fBTCL_CONTINUE\fR
  1066. Indicates that the \fBcontinue\fR command has been invoked, so the
  1067. innermost loop should go on to the next iteration.  The string
  1068. should always be empty.
  1069. .RE
  1070. Tcl programmers do not normally need to think about return codes,
  1071. since TCL_OK is almost always returned.  If anything else is returned
  1072. by a command, then the Tcl interpreter immediately stops processing
  1073. commands and returns to its caller.  If there are several nested
  1074. invocations of the Tcl interpreter in progress, then each nested
  1075. command will usually return the error to its caller, until eventually
  1076. the error is reported to the top-level application code.  The
  1077. application will then display the error message for the user.
  1078. .PP
  1079. In a few cases, some commands will handle certain ``error'' conditions
  1080. themselves and not return them upwards.  For example, the \fBfor\fR
  1081. command checks for the TCL_BREAK code; if it occurs, then \fBfor\fR
  1082. stops executing the body of the loop and returns TCL_OK to its
  1083. caller.  The \fBfor\fR command also handles TCL_CONTINUE codes and the
  1084. procedure interpreter handles TCL_RETURN codes.  The \fBcatch\fR
  1085. command allows Tcl programs to catch errors and handle them without
  1086. aborting command interpretation any further.
  1087.  
  1088. .SH PROCEDURES
  1089. .PP
  1090. Tcl allows you to extend the command interface by defining
  1091. procedures.  A Tcl procedure can be invoked just like any other Tcl
  1092. command (it has a name and it receives one or more arguments).
  1093. The only difference is that its body isn't a piece of C code linked
  1094. into the program; it is a string containing one or more other
  1095. Tcl commands.  See the \fBproc\fR command for information on
  1096. how to define procedures and what happens when they are invoked.
  1097.  
  1098. .SH VARIABLES \- SCALARS AND ARRAYS
  1099. .VS
  1100. .PP
  1101. Tcl allows the definition of variables and the use of their values
  1102. either through \fB$\fR-style variable substitution, the \fBset\fR
  1103. command, or a few other mechanisms.
  1104. Variables need not be declared:  a new variable will automatically
  1105. be created each time a new variable name is used.
  1106. .PP
  1107. Tcl supports two types of variables:  scalars and arrays.
  1108. A scalar variable has a single value, whereas an array variable
  1109. can have any number of elements, each with a name (called
  1110. its ``index'') and a value.
  1111. Array indexes may be arbitrary strings; they need not be numeric.
  1112. Parentheses are used refer to array elements in Tcl commands.
  1113. For example, the command
  1114. .DS C
  1115. \fBset x(first) 44\fR
  1116. .DE
  1117. will modify the element of \fBx\fR whose index is \fBfirst\fR
  1118. so that its new value is \fB44\fR.
  1119. Two-dimensional arrays can be simulated in Tcl by using indexes
  1120. that contain multiple concatenated values.
  1121. For example, the commands
  1122. .DS C
  1123. \fBset a(2,3) 1\fR
  1124. \fBset a(3,6) 2\fR
  1125. .DE
  1126. set the elements of \fBa\fR whose indexes are \fB2,3\fR and \fB3,6\fR.
  1127. .PP
  1128. In general, array elements may be used anywhere in Tcl that scalar
  1129. variables may be used.
  1130. If an array is defined with a particular name, then there may
  1131. not be a scalar variable with the same name.
  1132. Similarly, if there is a scalar variable with a particular
  1133. name then it is not possible to make array references to the
  1134. variable.
  1135. To convert a scalar variable to an array or vice versa, remove
  1136. the existing variable with the \fBunset\fR command.
  1137. .PP
  1138. The \fBarray\fR command provides several features for dealing
  1139. with arrays, such as querying the names of all the elements of
  1140. the array and searching through the array one element at a time.
  1141. .VE
  1142. .PP
  1143. Variables may be either global or local.  If a variable
  1144. name is used when a procedure isn't being executed, then it
  1145. automatically refers to a global variable.  Variable names used
  1146. within a procedure normally refer to local variables associated with that
  1147. invocation of the procedure.  Local variables are deleted whenever
  1148. a procedure exits.  The \fBglobal\fR command may be used to request
  1149. that a name refer to a global variable for the duration of the current
  1150. procedure (this is somewhat analogous to \fBextern\fR in C).
  1151.  
  1152. .SH "BUILT-IN COMMANDS"
  1153. .PP
  1154. The Tcl library provides the following built-in commands, which will
  1155. be available in any application using Tcl.  In addition to these
  1156. built-in commands, there may be additional commands defined by each
  1157. application, plus commands defined as Tcl procedures.
  1158. In the command syntax descriptions below, words in boldface are
  1159. literals that you type verbatim to Tcl.
  1160. Words in italics are meta-symbols; they serve as names for any of
  1161. a range of values that you can type.
  1162. Optional arguments or groups of arguments are indicated by enclosing them
  1163. in question-marks.
  1164. Ellipses (``...'') indicate that any number of additional
  1165. arguments or groups of arguments may appear, in the same format
  1166. as the preceding argument(s).
  1167. .TP
  1168. \fBappend \fIvarName value \fR?\fIvalue value ...\fR?
  1169. .VS
  1170. Append all of the \fIvalue\fR arguments to the current value
  1171. of variable \fIvarName\fR.  If \fIvarName\fR doesn't exist,
  1172. it is given a value equal to the concatenation of all the
  1173. \fIvalue\fR arguments.
  1174. This command provides an efficient way to build up long
  1175. variables incrementally.
  1176. For example, ``\fBappend a $b\fR'' is much more efficient than
  1177. ``\fBset a $a$b\fR'' if \fB$a\fR is long.
  1178. .VE
  1179. .TP
  1180. \fBarray \fIoption arrayName\fR ?\fIarg arg ...\fR?
  1181. .VS
  1182. This command performs one of several operations on the
  1183. variable given by \fIarrayName\fR.
  1184. \fIArrayName\fR must be the name of an existing array variable.
  1185. The \fIoption\fR argument determines what action is carried
  1186. out by the command.
  1187. The legal \fIoptions\fR (which may be abbreviated) are:
  1188. .RS
  1189. .TP
  1190. \fBarray anymore \fIarrayName searchId\fR
  1191. Returns 1 if there are any more elements left to be processed
  1192. in an array search, 0 if all elements have already been
  1193. returned.
  1194. \fISearchId\fR indicates which search on \fIarrayName\fR to
  1195. check, and must have been the return value from a previous
  1196. invocation of \fBarray startsearch\fR.
  1197. This option is particularly useful if an array has an element
  1198. with an empty name, since the return value from
  1199. \fBarray nextelement\fR won't indicate whether the search
  1200. has been completed.
  1201. .TP
  1202. \fBarray donesearch \fIarrayName searchId\fR
  1203. This command terminates an array search and destroys all the
  1204. state associated with that search.  \fISearchId\fR indicates
  1205. which search on \fIarrayName\fR to destroy, and must have
  1206. been the return value from a previous invocation of
  1207. \fBarray startsearch\fR.  Returns an empty string.
  1208. .TP
  1209. \fBarray names \fIarrayName\fR
  1210. Returns a list containing the names of all of the elements in
  1211. the array.
  1212. If there are no elements in the array then an empty string is
  1213. returned.
  1214. .TP
  1215. \fBarray nextelement \fIarrayName searchId\fR
  1216. Returns the name of the next element in \fIarrayName\fR, or
  1217. an empty string if all elements of \fIarrayName\fR have
  1218. already been returned in this search.  The \fIsearchId\fR
  1219. argument identifies the search, and must have
  1220. been the return value of an \fBarray startsearch\fR command.
  1221. Warning:  if elements are added to or deleted from the array,
  1222. then all searches are automatically terminated just as if
  1223. \fBarray donesearch\fR had been invoked; this will cause
  1224. \fBarray nextelement\fR operations to fail for those searches.
  1225. .TP
  1226. \fBarray size \fIarrayName\fR
  1227. Returns a decimal string giving the number of elements in the
  1228. array.
  1229. .TP
  1230. \fBarray startsearch \fIarrayName\fR
  1231. This command initializes an element-by-element search through the
  1232. array given by \fIarrayName\fR, such that invocations of the
  1233. \fBarray nextelement\fR command will return the names of the
  1234. individual elements in the array.
  1235. When the search has been completed, the \fBarray donesearch\fR
  1236. command should be invoked.
  1237. The return value is a
  1238. search identifier that must be used in \fBarray nextelement\fR
  1239. and \fBarray donesearch\fR commands; it allows multiple
  1240. searches to be underway simultaneously for the same array.
  1241. .VE
  1242. .RE
  1243. .TP
  1244. \fBbreak\fR
  1245. This command may be invoked only inside the body of a loop command
  1246. such as \fBfor\fR or \fBforeach\fR or \fBwhile\fR.  It returns a TCL_BREAK code
  1247. to signal the innermost containing loop command to return immediately.
  1248. .TP
  1249. \fBcase\fI string \fR?\fBin\fR? \fIpatList body \fR?\fIpatList body \fR...?
  1250. .TP
  1251. \fBcase\fI string \fR?\fBin\fR? {\fIpatList body \fR?\fIpatList body \fR...?}
  1252. Match \fIstring\fR against each of the \fIpatList\fR arguments
  1253. in order.  If one matches, then evaluate the following \fIbody\fR argument
  1254. by passing it recursively to the Tcl interpreter, and return the result
  1255. of that evaluation.  Each \fIpatList\fR argument consists of a single
  1256. pattern or list of patterns.  Each pattern may contain any of the wild-cards
  1257. described under \fBstring match\fR.  If a \fIpatList\fR
  1258. argument is \fBdefault\fR, the corresponding body will be evaluated
  1259. if no \fIpatList\fR matches \fIstring\fR.  If no \fIpatList\fR argument
  1260. matches \fIstring\fR and no default is given, then the \fBcase\fR
  1261. command returns an empty string.
  1262. .RS
  1263. .PP
  1264. Two syntaxes are provided.
  1265. The first uses a separate argument for each of the patterns and commands;
  1266. this form is convenient if substitutions are desired on some of the
  1267. patterns or commands.
  1268. .VS
  1269. The second form places all of the patterns and commands together into
  1270. a single argument; the argument must have proper list structure, with
  1271. the elements of the list being the patterns and commands.
  1272. The second form makes it easy to construct multi-line case commands,
  1273. since the braces around the whole list make it unnecessary to include a
  1274. backslash at the end of each line.
  1275. Since the \fIpatList\fR arguments are in braces in the second form,
  1276. no command or variable substitutions are performed on them;  this makes
  1277. the behavior of the second form different than the first form in some
  1278. cases.
  1279. .PP
  1280. Below are some examples of \fBcase\fR commands:
  1281. .DS
  1282. \fBcase abc in {a b} {format 1} default {format 2} a* {format 3}
  1283. .DE
  1284. will return \fB3\fR, 
  1285. .DS
  1286. .ta .5c 1c
  1287. \fBcase a in {
  1288.     {a b} {format 1}
  1289.     default {format 2}
  1290.     a* {format 3}
  1291. }
  1292. .DE
  1293. will return \fB1\fR, and
  1294. .DS
  1295. .ta .5c 1c
  1296. \fBcase xyz {
  1297.     {a b}
  1298.         {format 1}
  1299.     default
  1300.         {format 2}
  1301.     a*
  1302.         {format 3}
  1303. }
  1304. .DE
  1305. will return \fB2\fR.
  1306. .VE
  1307. .RE
  1308. .TP
  1309. \fBcatch\fI command \fR?\fIvarName\fR?
  1310. The \fBcatch\fR command may be used to prevent errors from aborting
  1311. command interpretation.  \fBCatch\fR calls the Tcl interpreter recursively
  1312. to execute \fIcommand\fR, and always returns a TCL_OK code, regardless of
  1313. any errors that might occur while executing \fIcommand\fR.  The return
  1314. value from \fBcatch\fR is a decimal string giving the
  1315. code returned by the Tcl interpreter after executing \fIcommand\fR.
  1316. This will be \fB0\fR (TCL_OK) if there were no errors in \fIcommand\fR; otherwise
  1317. it will have a non-zero value corresponding to one of the exceptional
  1318. return codes (see tcl.h for the definitions of code values).  If the
  1319. \fIvarName\fR argument is given, then it gives the name of a variable;
  1320. \fBcatch\fR will set the value of the variable to the string returned
  1321. from \fIcommand\fR (either a result or an error message).
  1322. .TP
  1323. \fBcd \fR?\fIdirName\fR?
  1324. .VS
  1325. Change the current working directory to \fIdirName\fR, or to the
  1326. home directory (as specified in the HOME environment variable) if
  1327. \fIdirName\fR is not given.
  1328. If \fIdirName\fR starts with a tilde, then tilde-expansion is
  1329. done as described for \fBTcl_TildeSubst\fR.
  1330. Returns an empty string.
  1331. This command can potentially be disruptive to an application,
  1332. so it may be removed in some applications.
  1333. .TP
  1334. \fBclose \fIfileId\fR
  1335. Closes the file given by \fIfileId\fR.
  1336. \fIFileId\fR must be the return value from a previous invocation
  1337. of the \fBopen\fR command; after this command, it should not be
  1338. used anymore.
  1339. If \fIfileId\fR refers to a command pipeline instead of a file,
  1340. then \fBclose\fR waits for the children to complete.
  1341. The normal result of this command is an empty string, but errors
  1342. are returned if there are problems in closing the file or waiting
  1343. for children to complete.
  1344. .VE
  1345. .TP
  1346. \fBconcat\fI arg \fR?\fIarg ...\fR?
  1347. This command treats each argument as a list and concatenates them
  1348. into a single list.  It permits any number of arguments.  For example,
  1349. the command
  1350. .RS
  1351. .DS
  1352. \fBconcat a b {c d e} {f {g h}}\fR
  1353. .DE
  1354. will return
  1355. .DS
  1356. \fBa b c d e f {g h}\fR
  1357. .DE
  1358. as its result.
  1359. .RE
  1360. .TP
  1361. \fBcontinue\fR
  1362. This command may be invoked only inside the body of a loop command
  1363. such as \fBfor\fR or \fBforeach\fR or \fBwhile\fR.  It
  1364. returns a  TCL_CONTINUE code
  1365. to signal the innermost containing loop command to skip the
  1366. remainder of the loop's body
  1367. but continue with the next iteration of the loop.
  1368. .TP
  1369. \fBeof \fIfileId\fR
  1370. .VS
  1371. Returns 1 if an end-of-file condition has occurred on \fIfileId\fR,
  1372. 0 otherwise.
  1373. \fIFileId\fR must have been the return
  1374. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  1375. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  1376. channels.
  1377. .VE
  1378. .TP
  1379. \fBerror \fImessage\fR ?\fIinfo\fR? ?\fIcode\fR?
  1380. Returns a TCL_ERROR code, which causes command interpretation to be
  1381. unwound.  \fIMessage\fR is a string that is returned to the application
  1382. to indicate what went wrong.
  1383. .RS
  1384. .PP
  1385. If the \fIinfo\fR argument is provided and is non-empty,
  1386. it is used to initialize the global variable \fBerrorInfo\fR.
  1387. \fBerrorInfo\fR is used to accumulate a stack trace of what
  1388. was in progress when an error occurred; as nested commands unwind,
  1389. the Tcl interpreter adds information to \fBerrorInfo\fR.  If the
  1390. \fIinfo\fR argument is present, it is used to initialize
  1391. \fBerrorInfo\fR and the first increment of unwind information
  1392. will not be added by the Tcl interpreter.  In other
  1393. words, the command containing the \fBerror\fR command will not appear
  1394. in \fBerrorInfo\fR; in its place will be \fIinfo\fR.
  1395. This feature is most useful in conjunction with the \fBcatch\fR command:
  1396. if a caught error cannot be handled successfully, \fIinfo\fR can be used
  1397. to return a stack trace reflecting the original point of occurrence
  1398. of the error:
  1399. .DS
  1400. \fBcatch {...} errMsg
  1401. set savedInfo $errorInfo
  1402. \&...
  1403. error $errMsg $savedInfo\fR
  1404. .DE
  1405. .PP
  1406. .VS
  1407. If the \fIcode\fR argument is present, then its value is stored
  1408. in the \fBerrorCode\fR global variable.  This variable is intended
  1409. to hold a machine-readable description of the error in cases where
  1410. such information is available; see the section BUILT-IN VARIABLES
  1411. below for information on the proper format for the variable.
  1412. If the \fIcode\fR argument is not
  1413. present, then \fBerrorCode\fR is automatically reset to
  1414. ``NONE'' by the Tcl interpreter as part of processing the
  1415. error generated by the command.
  1416. .VE
  1417. .RE
  1418. .TP
  1419. \fBeval \fIarg \fR?\fIarg ...\fR?
  1420. \fBEval\fR takes one or more arguments, which together comprise a Tcl
  1421. command (or collection of Tcl commands separated by newlines in the
  1422. usual way).  \fBEval\fR concatenates all its arguments in the same
  1423. fashion as the \fBconcat\fR command, passes the concatenated string to the
  1424. Tcl interpreter recursively, and returns the result of that
  1425. evaluation (or any error generated by it).
  1426. .TP
  1427. \fBexec \fIarg \fR?\fIarg ...\fR?
  1428. .VS
  1429. This command treats its arguments as the specification
  1430. of one or more UNIX commands to execute as subprocesses.
  1431. The commands take the form of a standard shell pipeline;
  1432. ``|'' arguments separate commands in the
  1433. pipeline and cause standard output of the preceding command
  1434. to be piped into standard input of the next command.
  1435. .RS
  1436. .PP
  1437. Under normal conditions the result of the \fBexec\fR command
  1438. consists of the standard output produced by the last command
  1439. in the pipeline.
  1440. If any of the commands in the pipeline exit abnormally or
  1441. are killed or suspended, then \fBexec\fR will return an error
  1442. and the error message will include the pipeline's output followed by
  1443. error messages describing the abnormal terminations; the
  1444. \fBerrorCode\fR variable will contain additional information
  1445. about the last abnormal termination encountered.
  1446. If any of the commands writes to its standard error file,
  1447. then \fBexec\fR will return an error, and the error message
  1448. will include the pipeline's output, followed by messages
  1449. about abnormal terminations (if any), followed by the standard error
  1450. output.
  1451. .PP
  1452. If the last character of the result or error message
  1453. is a newline then that character is deleted from the result
  1454. or error message for consistency with normal
  1455. Tcl return values.
  1456. .PP
  1457. If an \fIarg\fR has the value ``>'' then the
  1458. following argument is taken as the name of a file and
  1459. the standard output of the last command in the pipeline
  1460. is redirected to the file.  In this situation \fBexec\fR
  1461. will normally return an empty string.
  1462. .PP
  1463. If an \fIarg\fR has the value ``<'' then the following
  1464. argument is taken as the name of a file to use
  1465. for standard input to the first command in the
  1466. pipeline.
  1467. If an argument has the value ``<<'' then the following
  1468. argument is taken as an immediate value to be passed to
  1469. the first command as standard input.
  1470. If there is no ``<'' or ``<<'' argument then the standard
  1471. input for the first command in the pipeline is taken from
  1472. the application's current standard input.
  1473. .PP
  1474. If the last \fIarg\fR is ``&'' then the command will be
  1475. executed in background.
  1476. In this case the standard output from the last command
  1477. in the pipeline will
  1478. go to the application's standard output unless
  1479. redirected in the command, and error output from all
  1480. the commands in the pipeline will go to the application's
  1481. standard error file.
  1482. .PP
  1483. Each \fIarg\fR becomes one word for a command, except for
  1484. ``|'', ``<'', ``<<'', ``>'', and ``&'' arguments, and the
  1485. arguments that follow ``<'', ``<<'', and ``>''.
  1486. The first word in each command is taken as the command name;
  1487. tilde-substitution is performed on it, and the directories
  1488. in the PATH environment variable are searched for
  1489. an executable by the given name.
  1490. No ``glob'' expansion or other shell-like substitutions
  1491. are performed on the arguments to commands.
  1492. .RE
  1493. .TP
  1494. \fBexit \fR?returnCode\fR?
  1495. Terminate the process, returning \fIreturnCode\fR to the
  1496. parent as the exit status.
  1497. If \fIreturnCode\fR isn't specified then it defaults
  1498. to 0.
  1499. .VE
  1500. .TP
  1501. \fBexpr \fIarg\fR
  1502. Calls the expression processor to evaluate \fIarg\fR, and returns
  1503. the result as a string.  See the section EXPRESSIONS above.
  1504. .TP
  1505. \fBfile \fIoption\fR \fIname\fR ?\fIarg arg ...\fR?
  1506. .VS
  1507. Operate on a file or a file name.  \fIName\fR is the name of a file;
  1508. if it starts with a tilde, then tilde substitution is done before
  1509. executing the command (see the manual entry for \fBTcl_TildeSubst\fR
  1510. for details).
  1511. \fIOption\fR indicates what to do with the file name.  Any unique
  1512. abbreviation for \fIoption\fR is acceptable.  The valid options are:
  1513. .RS
  1514. .TP
  1515. \fBfile \fBatime \fIname\fR
  1516. Return a decimal string giving the time at which file \fIname\fR
  1517. was last accessed.  The time is measured in the standard UNIX
  1518. fashion as seconds from a fixed starting time (often January 1, 1970).
  1519. If the file doesn't exist or its access time cannot be queried then an
  1520. error is generated.
  1521. .TP
  1522. \fBfile \fBdirname \fIname\fR
  1523. Return all of the characters in \fIname\fR up to but not including
  1524. the last slash character.  If there are no slashes in \fIname\fR
  1525. then return ``.''.  If the last slash in \fIname\fR is its first
  1526. character, then return ``/''.
  1527. .TP
  1528. \fBfile \fBexecutable \fIname\fR
  1529. Return \fB1\fR if file \fIname\fR is executable by
  1530. the current user, \fB0\fR otherwise.
  1531. .TP
  1532. \fBfile \fBexists \fIname\fR
  1533. Return \fB1\fR if file \fIname\fR exists and the current user has
  1534. search privileges for the directories leading to it, \fB0\fR otherwise.
  1535. .TP
  1536. \fBfile \fBextension \fIname\fR
  1537. Return all of the characters in \fIname\fR after and including the
  1538. last dot in \fIname\fR.  If there is no dot in \fIname\fR then return
  1539. the empty string.
  1540. .TP
  1541. \fBfile \fBisdirectory \fIname\fR
  1542. Return \fB1\fR if file \fIname\fR is a directory,
  1543. \fB0\fR otherwise.
  1544. .TP
  1545. \fBfile \fBisfile \fIname\fR
  1546. Return \fB1\fR if file \fIname\fR is a regular file,
  1547. \fB0\fR otherwise.
  1548. .TP
  1549. \fBfile lstat \fIname varName\fR
  1550. Same as \fBstat\fR option (see below) except uses the \fIlstat\fR
  1551. kernel call instead of \fIstat\fR.  This means that if \fIname\fR
  1552. refers to a symbolic link the information returned in \fIvarName\fR
  1553. is for the link rather than the file it refers to.  On systems that
  1554. don't support symbolic links this option behaves exactly the same
  1555. as the \fBstat\fR option.
  1556. .TP
  1557. \fBfile \fBmtime \fIname\fR
  1558. Return a decimal string giving the time at which file \fIname\fR
  1559. was last modified.  The time is measured in the standard UNIX
  1560. fashion as seconds from a fixed starting time (often January 1, 1970).
  1561. If the file doesn't exist or its modified time cannot be queried then an
  1562. error is generated.
  1563. .TP
  1564. \fBfile \fBowned \fIname\fR
  1565. Return \fB1\fR if file \fIname\fR is owned by the current user,
  1566. \fB0\fR otherwise.
  1567. .TP
  1568. \fBfile \fBreadable \fIname\fR
  1569. Return \fB1\fR if file \fIname\fR is readable by
  1570. the current user, \fB0\fR otherwise.
  1571. .TP
  1572. \fBfile readlink \fIname\fR
  1573. Returns the value of the symbolic link given by \fIname\fR (i.e. the
  1574. name of the file it points to).  If
  1575. \fIname\fR isn't a symbolic link or its value cannot be read, then
  1576. an error is returned.  On systems that don't support symbolic links
  1577. this option is undefined.
  1578. .TP
  1579. \fBfile \fBrootname \fIname\fR
  1580. Return all of the characters in \fIname\fR up to but not including
  1581. the last ``.'' character in the name.  If \fIname\fR doesn't contain
  1582. a dot, then return \fIname\fR.
  1583. .TP
  1584. \fBfile \fBsize \fIname\fR
  1585. Return a decimal string giving the size of file \fIname\fR in bytes.
  1586. If the file doesn't exist or its size cannot be queried then an
  1587. error is generated.
  1588. .TP
  1589. \fBfile \fBstat  \fIname varName\fR
  1590. Invoke the \fBstat\fR kernel call on \fIname\fR, and use the
  1591. variable given by \fIvarName\fR to hold information returned from
  1592. the kernel call.
  1593. \fIVarName\fR is treated as an array variable,
  1594. and the following elements of that variable are set: \fBatime\fR,
  1595. \fBctime\fR, \fBdev\fR, \fBgid\fR, \fBino\fR, \fBmode\fR, \fBmtime\fR,
  1596. \fBnlink\fR, \fBsize\fR, \fBtype\fR, \fBuid\fR.
  1597. Each element except \fBtype\fR is a decimal string with the value of
  1598. the corresponding field from the \fBstat\fR return structure; see the
  1599. manual entry for \fBstat\fR for details on the meanings of the values.
  1600. The \fBtype\fR element gives the type of the file in the same form
  1601. returned by the command \fBfile type\fR.
  1602. This command returns an empty string.
  1603. .TP
  1604. \fBfile \fBtail \fIname\fR
  1605. Return all of the characters in \fIname\fR after the last slash.
  1606. If \fIname\fR contains no slashes then return \fIname\fR.
  1607. .TP
  1608. \fBfile \fBtype \fIname\fR
  1609. Returns a string giving the type of file \fIname\fR, which will be
  1610. one of \fBfile\fR, \fBdirectory\fR, \fBcharacterSpecial\fR,
  1611. \fBblockSpecial\fR, \fBfifo\fR, \fBlink\fR, or \fBsocket\fR.
  1612. .TP
  1613. \fBfile \fBwritable \fIname\fR
  1614. Return \fB1\fR if file \fIname\fR is writable by
  1615. the current user, \fB0\fR otherwise.
  1616. .RE
  1617. .IP
  1618. The \fBfile\fR commands that return 0/1 results are often used in
  1619. conditional or looping commands, for example:
  1620. .RS
  1621. .DS
  1622. \fBif {![file exists foo]} then {error {bad file name}} else {...}\fR
  1623. .DE
  1624. .VE
  1625. .RE
  1626. .TP
  1627. \fBflush \fIfileId\fR
  1628. .VS
  1629. Flushes any output that has been buffered for \fIfileId\fR.
  1630. \fIFileId\fR must have been the return
  1631. value from a previous call to \fBopen\fR, or it may be
  1632. \fBstdout\fR or \fBstderr\fR to access one of the standard I/O streams;
  1633. it must refer to a file that was opened for writing.
  1634. This command returns an empty string.
  1635. .VE
  1636. .TP
  1637. \fBfor \fIstart test next body\fR
  1638. \fBFor\fR is a looping command, similar in structure to the C
  1639. \fBfor\fR statement.  The \fIstart\fR, \fInext\fR, and
  1640. \fIbody\fR arguments must be Tcl command strings, and \fItest\fR
  1641. is an expression string.
  1642. The \fBfor\fR command first invokes the Tcl interpreter to
  1643. execute \fIstart\fR.  Then it repeatedly evaluates \fItest\fR as
  1644. an expression; if the result is non-zero it invokes the Tcl
  1645. interpreter on \fIbody\fR, then invokes the Tcl interpreter on \fInext\fR,
  1646. then repeats the loop.  The command terminates when \fItest\fR evaluates
  1647. to 0.  If a \fBcontinue\fR command is invoked within \fIbody\fR then
  1648. any remaining commands in the current execution of \fIbody\fR are skipped;
  1649. processing continues by invoking the Tcl interpreter on \fInext\fR, then
  1650. evaluating \fItest\fR, and so on.  If a \fBbreak\fR command is invoked
  1651. within \fIbody\fR
  1652. or \fInext\fR,
  1653. then the \fBfor\fR command will
  1654. return immediately.
  1655. The operation of \fBbreak\fR and \fBcontinue\fR are similar to the
  1656. corresponding statements in C.
  1657. \fBFor\fR returns an empty string.
  1658. .TP
  1659. \fBforeach \fIvarname list body\fR
  1660. In this command, \fIvarname\fR is the name of a variable, \fIlist\fR
  1661. is a list of values to assign to \fIvarname\fR, and \fIbody\fR is a
  1662. collection of Tcl commands.  For each field in \fIlist\fR (in order
  1663. from left to right), \fBforeach\fR assigns the contents of the
  1664. field to \fIvarname\fR (as if the \fBlindex\fR command had been used
  1665. to extract the field), then calls the Tcl interpreter to execute
  1666. \fIbody\fR.  The \fBbreak\fR and \fBcontinue\fR statements may be
  1667. invoked inside \fIbody\fR, with the same effect as in the \fBfor\fR
  1668. command.  \fBForeach\fR returns an empty string.
  1669. .TP
  1670. \fBformat \fIformatString \fR?\fIarg arg ...\fR?
  1671. This command generates a formatted string in the same way as the
  1672. C \fBsprintf\fR procedure (it uses \fBsprintf\fR in its
  1673. implementation).  \fIFormatString\fR indicates how to format
  1674. the result, using \fB%\fR fields as in \fBsprintf\fR, and the additional
  1675. arguments, if any, provide values to be substituted into the result.
  1676. All of the \fBsprintf\fR options are valid; see the \fBsprintf\fR
  1677. man page for details.  Each \fIarg\fR must match the expected type
  1678. from the \fB%\fR field in \fIformatString\fR; the \fBformat\fR command
  1679. converts each argument to the correct type (floating, integer, etc.)
  1680. before passing it to \fBsprintf\fR for formatting.
  1681. The only unusual conversion is for \fB%c\fR; in this case the argument
  1682. must be a decimal string, which will then be converted to the corresponding
  1683. ASCII character value.
  1684. \fBFormat\fR does backslash substitution on its \fIformatString\fR
  1685. argument, so backslash sequences in \fIformatString\fR will be handled
  1686. correctly even if the argument is in braces.
  1687. The return value from \fBformat\fR
  1688. is the formatted string.
  1689. .TP
  1690. \fBgets \fIfileId\fR ?\fIvarName\fR?
  1691. .VS
  1692. Reads the next line from the file given by \fIfileId\fR and discards
  1693. the terminating newline character.
  1694. If \fIvarName\fR is specified, then the line is placed in the variable
  1695. by that name and the return value is a count of the number of characters
  1696. read (not including the newline).
  1697. If the end of the file is reached before reading
  1698. any characters then \-1 is returned and \fIvarName\fR is set to an
  1699. empty string.
  1700. If \fIvarName\fR is not specified then the return value will be
  1701. the line (minus the newline character) or an empty string if
  1702. the end of the file is reached before reading any characters.
  1703. An empty string will also be returned if a line contains no characters
  1704. except the newline, so \fBeof\fR may have to be used to determine
  1705. what really happened.
  1706. If the last character in the file is not a newline character, then
  1707. \fBgets\fR behaves as if there were an additional newline character
  1708. at the end of the file.
  1709. \fIFileId\fR must be \fBstdin\fR or the return value from a previous
  1710. call to \fBopen\fR; it must refer to a file that was opened
  1711. for reading.
  1712. .VE
  1713. .TP
  1714. \fBglob \fR?\fB\-nocomplain\fR? \fIfilename\fR ?\fIfilename ...\fR?
  1715. This command performs filename globbing, using csh rules.  The returned
  1716. value from \fBglob\fR is the list of expanded filenames.
  1717. .VS
  1718. If \fB\-nocomplain\fR is specified as the first argument then an empty
  1719. list may be returned;  otherwise an error is returned if the expanded
  1720. list is empty.  The \fB\-nocomplain\fR argument must be provided
  1721. exactly: an abbreviation will not be accepted.
  1722. .VE
  1723. .TP
  1724. \fBglobal \fIvarname \fR?\fIvarname ...\fR?
  1725. This command is ignored unless a Tcl procedure is being interpreted.
  1726. If so, then it declares the given \fIvarname\fR's to be global variables
  1727. rather than local ones.  For the duration of the current procedure
  1728. (and only while executing in the current procedure), any reference to
  1729. any of the \fIvarname\fRs will be bound to a global variable instead
  1730. of a local one.
  1731. .TP
  1732. \fBhistory \fR?\fIoption\fR? ?\fIarg arg ...\fR?
  1733. Note:  this command may not be available in all Tcl-based applications.
  1734. Typically, only those that receive command input in a typescript
  1735. form will support history.
  1736. The \fBhistory\fR command performs one of several operations related to
  1737. recently-executed commands recorded in a history list.  Each of
  1738. these recorded commands is referred to as an ``event''.  When
  1739. specifying an event to the \fBhistory\fR command, the following
  1740. forms may be used:
  1741. .RS
  1742. .IP [1]
  1743. A number:  if positive, it refers to the event with
  1744. that number (all events are numbered starting at 1).  If the number
  1745. is negative, it selects an event relative to the current event
  1746. (\fB\-1\fR refers to the previous event, \fB\-2\fR to the one before that, and
  1747. so on).
  1748. .IP [2]
  1749. A string:  selects the most recent event that matches the string.
  1750. An event is considered to match the string either if the string is
  1751. the same as the first characters of the event, or if the string
  1752. matches the event in the sense of the \fBstring match\fR command.
  1753. .LP
  1754. The \fBhistory\fR command can take any of the following forms:
  1755. .TP
  1756. \fBhistory\fR
  1757. Same
  1758. .VS
  1759. as \fBhistory info\fR, described below.
  1760. .VE
  1761. .TP
  1762. \fBhistory add\fI command \fR?\fBexec\fR?
  1763. Add the \fIcommand\fR argument to the history list as a new event.  If
  1764. \fBexec\fR is specified (or abbreviated) then the command is also
  1765. executed and its result is returned.  If \fBexec\fR isn't specified
  1766. then an empty string is returned as result.
  1767. .TP
  1768. \fBhistory change\fI newValue\fR ?\fIevent\fR?
  1769. Replace the value recorded for an event with \fInewValue\fR.  \fIEvent\fR
  1770. specifies the event to replace, and
  1771. defaults to the \fIcurrent\fR event (not event \fB\-1\fR).  This command
  1772. is intended for use in commands that implement new forms of history
  1773. substitution and wish to replace the current event (which invokes the
  1774. substitution) with the command created through substitution.  The return
  1775. value is an empty string.
  1776. .TP
  1777. \fBhistory event\fR ?\fIevent\fR?
  1778. Returns the value of the event given by \fIevent\fR.  \fIEvent\fR
  1779. defaults to \fB\-1\fR.  This command causes history revision to occur:
  1780. see below for details.
  1781. .TP
  1782. \fBhistory info \fR?\fIcount\fR?
  1783. Returns a formatted string (intended for humans to read) giving
  1784. the event number and contents for each of the events in the history
  1785. list except the current event.  If \fIcount\fR is specified
  1786. then only the most recent \fIcount\fR events are returned.
  1787. .TP
  1788. \fBhistory keep \fIcount\fR
  1789. This command may be used to change the size of the history list to
  1790. \fIcount\fR events.  Initially, 20 events are retained in the history
  1791. list.  This command returns an empty string.
  1792. .TP
  1793. \fBhistory nextid\fR
  1794. Returns the number of the next event to be recorded
  1795. in the history list.  It is useful for things like printing the
  1796. event number in command-line prompts.
  1797. .TP
  1798. \fBhistory redo \fR?\fIevent\fR?
  1799. Re-execute the command indicated by \fIevent\fR and return its result.
  1800. \fIEvent\fR defaults to \fB\-1\fR.  This command results in history
  1801. revision:  see below for details.
  1802. .TP
  1803. \fBhistory substitute \fIold new \fR?\fIevent\fR?
  1804. Retrieve the command given by \fIevent\fR
  1805. (\fB\-1\fR by default), replace any occurrences of \fIold\fR by
  1806. \fInew\fR in the command (only simple character equality is supported;
  1807. no wild cards), execute the resulting command, and return the result
  1808. of that execution.  This command results in history
  1809. revision:  see below for details.
  1810. .TP
  1811. \fBhistory words \fIselector\fR ?\fIevent\fR?
  1812. Retrieve from the command given by \fIevent\fR (\fB\-1\fR by default)
  1813. the words given by \fIselector\fR, and return those words in a string
  1814. separated by spaces.  The \fBselector\fR argument has three forms.
  1815. If it is a single number then it selects the word given by that
  1816. number (\fB0\fR for the command name, \fB1\fR for its first argument,
  1817. and so on).  If it consists of two numbers separated by a dash,
  1818. then it selects all the arguments between those two.  Otherwise
  1819. \fBselector\fR is treated as a pattern; all words matching that
  1820. pattern (in the sense of \fBstring match\fR) are returned.  In
  1821. the numeric forms \fB$\fR may be used
  1822. to select the last word of a command.
  1823. For example, suppose the most recent command in the history list is
  1824. .RS
  1825. .DS
  1826. \fBformat  {%s is %d years old} Alice [expr $ageInMonths/12]\fR
  1827. .DE
  1828. Below are some history commands and the results they would produce:
  1829. .DS
  1830. .ta 4c
  1831. .fi
  1832. .UL Command "    "
  1833. .UL Result
  1834. .nf
  1835.  
  1836. \fBhistory words $    [expr $ageInMonths/12]\fR
  1837. \fBhistory words 1-2    {%s is %d years  old} Alice\fR
  1838. \fBhistory words *a*o*    {%s is %d years old} [expr $ageInMonths/12]\fR
  1839. .DE
  1840. \fBHistory words\fR results in history revision:  see below for details.
  1841. .RE
  1842. The history options \fBevent\fR, \fBredo\fR, \fBsubstitute\fR,
  1843. and \fBwords\fR result in ``history revision''.
  1844. When one of these options is invoked then the current event
  1845. is modified to eliminate the history command and replace it with
  1846. the result of the history command.
  1847. For example, suppose that the most recent command in the history
  1848. list is
  1849. .DS
  1850. \fBset a [expr $b+2]\fR
  1851. .DE
  1852. and suppose that the next command invoked is one of the ones on
  1853. the left side of the table below.  The command actually recorded in
  1854. the history event will be the corresponding one on the right side
  1855. of the table.
  1856. .ne 1.5c
  1857. .DS
  1858. .ta 4c
  1859. .fi
  1860. .UL "Command Typed" "    "
  1861. .UL "Command Recorded"
  1862. .nf
  1863.  
  1864. \fBhistory    set a [expr $b+2]\fR
  1865. \fBhistory s a b    set b [expr $b+2]\fR
  1866. \fBset c [history w 2]    set c [expr $b+2]\fR
  1867. .DE
  1868. .VS
  1869. History revision is needed because event specifiers like \fB\-1\fR
  1870. are only valid at a particular time:  once more events have been
  1871. added to the history list a different event specifier would be
  1872. needed.
  1873. History revision occurs even when \fBhistory\fR is invoked
  1874. indirectly from the current event (e.g. a user types a command
  1875. that invokes a Tcl procedure that invokes \fBhistory\fR):  the
  1876. top-level command whose execution eventually resulted in a
  1877. \fBhistory\fR command is replaced.
  1878. If you wish to invoke commands like \fBhistory words\fR without
  1879. history revision, you can use \fBhistory event\fR to save the
  1880. current history event and then use \fBhistory change\fR to
  1881. restore it later.
  1882. .VE
  1883. .VE
  1884. .RE
  1885. .TP
  1886. \fBif \fItest \fR?\fBthen\fR? \fItrueBody \fR?\fBelse\fR? ?\fIfalseBody\fR?
  1887. The \fIif\fR command evaluates \fItest\fR as an expression (in the
  1888. same way that \fBexpr\fR evaluates its argument).  The value of the
  1889. expression must be numeric; if it
  1890. is non-zero then \fItrueBody\fR is called by passing it to the
  1891. Tcl interpreter.  Otherwise \fIfalseBody\fR is executed by passing it to
  1892. the Tcl interpreter.  The \fBthen\fR and \fBelse\fR arguments are optional
  1893. ``noise words'' to make the command easier to read.  \fIFalseBody\fR is
  1894. also optional; if it isn't specified then the command does nothing if
  1895. \fItest\fR evaluates to zero.  The return value from \fBif\fR is
  1896. the value of the last command executed in \fItrueBody\fR or
  1897. \fIfalseBody\fR, or the empty string if \fItest\fR evaluates to zero and
  1898. \fIfalseBody\fR isn't specified.
  1899. .TP
  1900. \fBincr \fIvarName \fR?\fIincrement\fR?
  1901. .VS
  1902. Increment the value stored in the variable whose name is \fIvarName\fR.
  1903. The value of the variable must be integral.
  1904. If \fIincrement\fR is supplied then its value (which must be an
  1905. integer) is added to the value of variable \fIvarName\fR;  otherwise
  1906. 1 is added to \fIvarName\fR.
  1907. The new value is stored as a decimal string in variable \fIvarName\fR
  1908. and also returned as result.
  1909. .VE
  1910. .TP
  1911. \fBinfo \fIoption \fR?\fIarg arg ...\fR?
  1912. Provide information about various internals to the Tcl interpreter.
  1913. The legal \fIoption\fR's (which may be abbreviated) are:
  1914. .RS
  1915. .TP
  1916. \fBinfo args \fIprocname\fR
  1917. Returns a list containing the names of the arguments to procedure
  1918. \fIprocname\fR, in order.  \fIProcname\fR must be the name of a
  1919. Tcl command procedure.
  1920. .TP
  1921. \fBinfo body \fIprocname\fR
  1922. Returns the body of procedure \fIprocname\fR.  \fIProcname\fR must be
  1923. the name of a Tcl command procedure.
  1924. .TP
  1925. \fBinfo cmdcount\fR
  1926. Returns a count of the total number of commands that have been invoked
  1927. in this interpreter.
  1928. .TP
  1929. \fBinfo commands \fR?\fIpattern\fR?
  1930. If \fIpattern\fR isn't specified, returns a list of names of all the
  1931. Tcl commands, including both the built-in commands written in C and
  1932. the command procedures defined using the \fBproc\fR command.
  1933. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1934. are returned.  Matching is determined using the same rules as for
  1935. \fBstring match\fR.
  1936. .TP
  1937. \fBinfo default \fIprocname arg varname\fR
  1938. \fIProcname\fR must be the name of a Tcl command procedure and \fIarg\fR
  1939. must be the name of an argument to that procedure.  If \fIarg\fR
  1940. doesn't have a default value then the command returns \fB0\fR.
  1941. Otherwise it returns \fB1\fR and places the default value of \fIarg\fR
  1942. into variable \fIvarname\fR.
  1943. .TP
  1944. \fBinfo exists \fIvarName\fR
  1945. Returns \fB1\fR if the variable named \fIvarName\fR exists in the
  1946. current context (either as a global or local variable), returns \fB0\fR
  1947. otherwise.
  1948. .TP
  1949. \fBinfo globals \fR?\fIpattern\fR?
  1950. If \fIpattern\fR isn't specified, returns a list of all the names
  1951. of currently-defined global variables.
  1952. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1953. are returned.  Matching is determined using the same rules as for
  1954. \fBstring match\fR.
  1955. .TP
  1956. \fBinfo level\fR ?\fInumber\fR?
  1957. If \fInumber\fR is not specified, this command returns a number
  1958. giving the stack level of the invoking procedure, or 0 if the
  1959. command is invoked at top-level.  If \fInumber\fR is specified,
  1960. then the result is a list consisting of the name and arguments for the
  1961. procedure call at level \fInumber\fR on the stack.  If \fInumber\fR
  1962. is positive then it selects a particular stack level (1 refers
  1963. to the top-most active procedure, 2 to the procedure it called, and
  1964. so on); otherwise it gives a level relative to the current level
  1965. (0 refers to the current procedure, -1 to its caller, and so on).
  1966. See the \fBuplevel\fR command for more information on what stack
  1967. levels mean.
  1968. .TP
  1969. \fBinfo library\fR
  1970. .VS
  1971. Returns the name of the library directory in which standard Tcl
  1972. scripts are stored.
  1973. If there is no such directory defined for the current installation
  1974. then an error is generated.
  1975. See the \fBlibrary\fR manual entry for details of the facilities
  1976. provided by the Tcl script library.
  1977. Normally each application will have its own application-specific
  1978. script library in addition to the Tcl script library;  I suggest that
  1979. each application set a global variable with a name like
  1980. \fB$\fIapp\fBLibrary\fR (where \fIapp\fR is the application's name)
  1981. to hold the location of that application's library directory.
  1982. .VE
  1983. .TP
  1984. \fBinfo locals \fR?\fIpattern\fR?
  1985. If \fIpattern\fR isn't specified, returns a list of all the names
  1986. of currently-defined local variables, including arguments to the
  1987. current procedure, if any.
  1988. .VS
  1989. Variables defined with the \fBglobal\fR and \fBupvar\fR commands
  1990. will not be returned.
  1991. .VE
  1992. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1993. are returned.  Matching is determined using the same rules as for
  1994. \fBstring match\fR.
  1995. .TP
  1996. \fBinfo procs \fR?\fIpattern\fR?
  1997. If \fIpattern\fR isn't specified, returns a list of all the
  1998. names of Tcl command procedures.
  1999. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  2000. are returned.  Matching is determined using the same rules as for
  2001. \fBstring match\fR.
  2002. .TP
  2003. \fBinfo script\fR
  2004. .VS
  2005. If a Tcl script file is currently being evaluated (i.e. there is a
  2006. call to \fBTcl_EvalFile\fR active or there is an active invocation
  2007. of the \fBsource\fR command), then this command returns the name
  2008. of the innermost file being processed.  Otherwise the command returns an
  2009. empty string.
  2010. .VE
  2011. .TP
  2012. \fBinfo tclversion\fR
  2013. Returns the version number for this version of Tcl in the form \fIx.y\fR,
  2014. where changes to \fIx\fR represent major changes with probable
  2015. incompatibilities and changes to \fIy\fR represent small enhancements and
  2016. bug fixes that retain backward compatibility.
  2017. .TP
  2018. \fBinfo vars\fR ?\fIpattern\fR?
  2019. If \fIpattern\fR isn't specified,
  2020. returns a list of all the names of currently-visible variables, including
  2021. both locals and currently-visible globals.
  2022. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  2023. are returned.  Matching is determined using the same rules as for
  2024. \fBstring match\fR.
  2025. .RE
  2026. .TP
  2027. \fBjoin \fIlist \fR?\fIjoinString\fR?
  2028. .VS
  2029. The \fIlist\fR argument must be a valid Tcl list.
  2030. This command returns the string
  2031. formed by joining all of the elements of \fIlist\fR together with
  2032. \fIjoinString\fR separating each adjacent pair of elements.
  2033. The \fIjoinString\fR argument defaults to a space character.
  2034. .VE
  2035. .TP
  2036. \fBlappend \fIvarName value \fR?\fIvalue value ...\fR?
  2037. .VS
  2038. Treat the variable given by \fIvarName\fR as a list and append
  2039. each of the \fIvalue\fR arguments to that list as a separate
  2040. element, with spaces between elements.
  2041. If \fIvarName\fR doesn't exist, it is created as a list with elements
  2042. given by the \fIvalue\fR arguments.
  2043. \fBLappend\fR is similar to \fBappend\fR except that the \fIvalue\fRs
  2044. are appended as list elements rather than raw text.
  2045. This command provides a relatively efficient way to build up
  2046. large lists.  For example, ``\fBlappend a $b\fR'' is much
  2047. more efficient than ``\fBset a [concat $a [list $b]]\fR'' when
  2048. \fB$a\fR is long.
  2049. .TP
  2050. \fBlindex \fIlist index\fR
  2051. Treats \fIlist\fR as a Tcl list and returns the \fIindex\fR'th element
  2052. from it (0 refers to the first element of the list).
  2053. In extracting the element, \fIlindex\fR observes the same rules
  2054. concerning braces and quotes and backslashes as the Tcl command
  2055. interpreter; however, variable
  2056. substitution and command substitution do not occur.
  2057. If \fIindex\fR is negative or greater than or equal to the number
  2058. of elements in \fIvalue\fR, then an empty
  2059. string is returned.
  2060. .TP
  2061. \fBlinsert \fIlist index element \fR?\fIelement element ...\fR?
  2062. This command produces a new list from \fIlist\fR by inserting all
  2063. of the \fIelement\fR arguments just before the \fIindex\fRth
  2064. element of \fIlist\fR.  Each \fIelement\fR argument will become
  2065. a separate element of the new list.  If \fIindex\fR is less than
  2066. or equal to zero, then the new elements are inserted at the
  2067. beginning of the list.  If \fIindex\fR is greater than or equal
  2068. to the number of elements in the list, then the new elements are
  2069. appended to the list.
  2070. .VE
  2071. .TP
  2072. \fBlist \fIarg \fR?\fIarg ...\fR?
  2073. This command returns a list comprised of all the \fIarg\fRs.  Braces
  2074. and backslashes get added as necessary, so that the \fBindex\fR command
  2075. may be used on the result to re-extract the original arguments, and also
  2076. so that \fBeval\fR may be used to execute the resulting list, with
  2077. \fIarg1\fR comprising the command's name and the other \fIarg\fRs comprising
  2078. its arguments.  \fBList\fR produces slightly different results than
  2079. \fBconcat\fR:  \fBconcat\fR removes one level of grouping before forming
  2080. the list, while \fBlist\fR works directly from the original arguments.
  2081. For example, the command
  2082. .RS
  2083. .DS
  2084. \fBlist a b {c d e} {f {g h}}
  2085. .DE
  2086. will return
  2087. .DS
  2088. \fBa b {c d e} {f {g h}}
  2089. .DE
  2090. while \fBconcat\fR with the same arguments will return
  2091. .DS
  2092. \fBa b c d e f {g h}\fR
  2093. .DE
  2094. .RE
  2095. .br
  2096. .VS
  2097. .TP
  2098. \fBllength \fIlist\fR
  2099. Treats \fIlist\fR as a list and returns a decimal string giving
  2100. the number of elements in it.
  2101. .TP
  2102. \fBlrange \fIlist first last
  2103. \fIList\fR must be a valid Tcl list.  This command will
  2104. return a new list consisting of elements
  2105. \fIfirst\fR through \fIlast\fR, inclusive.
  2106. \fILast\fR may be \fBend\fR (or any
  2107. abbreviation of it) to refer to the last element of the list.
  2108. If \fIfirst\fR is less than zero, it is treated as if it were zero.
  2109. If \fIlast\fR is greater than or equal to the number of elements
  2110. in the list, then it is treated as if it were \fBend\fR.
  2111. If \fIfirst\fR is greater than \fIlast\fR then an empty string
  2112. is returned.
  2113. Note: ``\fBlrange \fIlist first first\fR'' does not always produce the
  2114. same result as ``\fBlindex \fIlist first\fR'' (although it often does
  2115. for simple fields that aren't enclosed in braces); it does, however,
  2116. produce exactly the same results as ``\fBlist [lindex \fIlist first\fB]\fR''
  2117. .TP
  2118. \fBlreplace \fIlist first last \fR?\fIelement element ...\fR?
  2119. Returns a new list formed by replacing one or more elements of
  2120. \fIlist\fR with the \fIelement\fR arguments.
  2121. \fIFirst\fR gives the index in \fIlist\fR of the first element
  2122. to be replaced.
  2123. If \fIfirst\fR is less than zero then it refers to the first
  2124. element of \fIlist\fR;  the element indicated by \fIfirst\fR
  2125. must exist in the list.
  2126. \fILast\fR gives the index in \fIlist\fR of the last element
  2127. to be replaced;  it must be greater than or equal to \fIfirst\fR.
  2128. \fILast\fR may be \fBend\fR (or any abbreviation of it) to indicate
  2129. that all elements between \fIfirst\fR and the end of the list should
  2130. be replaced.
  2131. The \fIelement\fR arguments specify zero or more new arguments to
  2132. be added to the list in place of those that were deleted.
  2133. Each \fIelement\fR argument will become a separate element of
  2134. the list.
  2135. If no \fIelement\fR arguments are specified, then the elements
  2136. between \fIfirst\fR and \fIlast\fR are simply deleted.
  2137. .TP
  2138. \fBlsearch \fIlist pattern\fR
  2139. Search the elements of \fIlist\fR to see if one of them matches
  2140. \fIpattern\fR.
  2141. If so, the command returns the index of the first matching
  2142. element.
  2143. If not, the command returns \fB\-1\fR.
  2144. Pattern matching is done in the same way as for the \fBstring match\fR
  2145. command.
  2146. .TP
  2147. \fBlsort \fIlist\fR
  2148. Sort the elements of \fIlist\fR, returning a new list in sorted
  2149. order.
  2150. ASCII sorting is used, with the result in increasing order.
  2151. .VE
  2152. .TP
  2153. \fBopen \fIfileName\fR ?\fIaccess\fR?
  2154. .VS
  2155. Opens a file and returns an identifier
  2156. that may be used in future invocations
  2157. of commands like \fBread\fR, \fBputs\fR, and \fBclose\fR.
  2158. \fIFileName\fR gives the name of the file to open; if it starts with
  2159. a tilde then tilde substitution is performed as described for
  2160. \fBTcl_TildeSubst\fR.
  2161. If the first character of \fIfileName\fR is ``|'' then the
  2162. remaining characters of \fIfileName\fR are treated as a command
  2163. pipeline to invoke, in the same style as for \fBexec\fR.
  2164. In this case, the identifier returned by \fBopen\fR may be used
  2165. to write to the command's input pipe or read from its output pipe.
  2166. The \fIaccess\fR argument indicates the way in which the file
  2167. (or command pipeline) is to be accessed.
  2168. It may have any of the following values:
  2169. .RS
  2170. .TP
  2171. \fBr\fR
  2172. Open the file for reading only; the file must already exist.
  2173. .TP
  2174. \fBr+\fR
  2175. Open the file for both reading and writing; the file must
  2176. already exist.
  2177. .TP
  2178. \fBw\fR
  2179. Open the file for writing only.  Truncate it if it exists.  If it doesn't
  2180. exist, create a new file.
  2181. .TP
  2182. \fBw+\fR
  2183. Open the file for reading and writing.  Truncate it if it exists.
  2184. If it doesn't exist, create a new file.
  2185. .TP
  2186. \fBa\fR
  2187. Open the file for writing only.  The file must already exist, and the file
  2188. is positioned so that new data is appended to the file.
  2189. .TP
  2190. \fBa+\fR
  2191. Open the file for reading and writing.  The file must already exist, and
  2192. the initial access position is set to the end of the file.
  2193. .PP
  2194. \fIAccess\fR defaults to \fBr\fR.
  2195. If a file is opened for both reading and writing, then \fBseek\fR
  2196. must be invoked between a read and a write, or vice versa (this
  2197. restriction does not apply to command pipelines opened with \fBopen\fR).
  2198. When \fIfileName\fR specifies a command pipeline and a write-only access
  2199. is used, then standard output from the pipeline is directed to the
  2200. current standard output unless overridden by the command.
  2201. When \fIfileName\fR specifies a command pipeline and a read-only access
  2202. is used, then standard input from the pipeline is taken from the
  2203. current standard input unless overridden by the command.
  2204. .RE
  2205. .VE
  2206. .TP
  2207. \fBproc \fIname args body\fR
  2208. The \fBproc\fR command creates a new Tcl command procedure,
  2209. \fIname\fR, replacing
  2210. any existing command there may have been by that name.  Whenever the
  2211. new command is invoked, the contents of \fIbody\fR will be executed
  2212. by the Tcl interpreter.  \fIArgs\fR specifies the formal arguments to the
  2213. procedure.  It consists of a list, possibly empty, each of whose
  2214. elements specifies
  2215. one argument.  Each argument specifier is also a list with either
  2216. one or two fields.  If there is only a single field in the specifier,
  2217. then it is the name of the argument; if there are two fields, then
  2218. the first is the argument name and the second is its default value.
  2219. braces and backslashes may be used in the usual way to specify
  2220. complex default values.
  2221. .IP
  2222. When \fIname\fR is invoked, a local variable
  2223. will be created for each of the formal arguments to the procedure; its
  2224. value will be the value of corresponding argument in the invoking command
  2225. or the argument's default value.
  2226. Arguments with default values need not be
  2227. specified in a procedure invocation.  However, there must be enough
  2228. actual arguments for all the
  2229. formal arguments that don't have defaults, and there must not be any extra
  2230. actual arguments.  There is one special case to permit procedures with
  2231. variable numbers of arguments.  If the last formal argument has the name
  2232. \fBargs\fR, then a call to the procedure may contain more actual arguments
  2233. than the procedure has formals.  In this case, all of the actual arguments
  2234. starting at the one that would be assigned to \fBargs\fR are combined into
  2235. a list (as if the \fBlist\fR command had been used); this combined value
  2236. is assigned to the local variable \fBargs\fR.
  2237. .IP
  2238. When \fIbody\fR is being executed, variable names normally refer to
  2239. local variables, which are created automatically when referenced and
  2240. deleted when the procedure returns.  One local variable is automatically
  2241. created for each of the procedure's arguments.
  2242. Global variables can only be accessed by invoking
  2243. the \fBglobal\fR command.
  2244. .IP
  2245. The \fBproc\fR command returns the null string.  When a procedure is
  2246. invoked, the procedure's return value is the value specified in a
  2247. \fBreturn\fR command.  If the procedure doesn't execute an explicit
  2248. \fBreturn\fR, then its return value is the value of the last command
  2249. executed in the procedure's body.
  2250. If an error occurs while executing the procedure
  2251. body, then the procedure-as-a-whole will return that same error.
  2252. .TP
  2253. \fBputs \fIfileId string \fR?\fBnonewline\fR?
  2254. .VS
  2255. Writes the characters given by \fIstring\fR to the file given
  2256. by \fIfileId\fR.
  2257. \fBPuts\fR normally outputs a newline character after \fIstring\fR,
  2258. but this feature may be suppressed by specifying the \fBnonewline\fR
  2259. argument.
  2260. Output to files is buffered internally by Tcl; the \fBflush\fR
  2261. command may be used to force buffered characters to be output.
  2262. \fIFileId\fR must have been the return
  2263. value from a previous call to \fBopen\fR, or it may be
  2264. \fBstdout\fR or \fBstderr\fR to refer to one of the standard I/O
  2265. channels; it must refer to a file that was opened for
  2266. writing.
  2267. .TP
  2268. \fBpwd\fR
  2269. .br
  2270. Returns the path name of the current working directory.
  2271. .TP
  2272. \fBread \fIfileId\fR
  2273. .TP
  2274. \fBread \fIfileId \fBnonewline\fR
  2275. .TP
  2276. \fBread \fIfileId numBytes\fR
  2277. In the first form, all of the remaining bytes are read from the file
  2278. given by \fIfileId\fR; they are returned as the result of the command.
  2279. If \fBnonewline\fR is specified as an additional argument, then the last
  2280. character of the file is discarded if it is a newline.
  2281. In the third form, the extra argument specifies how many bytes to read;
  2282. exactly this many bytes will be read and returned, unless there are fewer than
  2283. \fInumBytes\fR bytes left in the file; in this case, all the remaining
  2284. bytes are returned.
  2285. \fIFileId\fR must be \fBstdin\fR or the return
  2286. value from a previous call to \fBopen\fR; it must
  2287. refer to a file that was opened for reading.
  2288. .TP
  2289. \fBregexp \fR?\fB\-indices\fR? \fR?\fB\-nocase\fR? \fIexp string \fR?\fImatchVar\fR? ?\fIsubMatchVar subMatchVar ...\fR?
  2290. Determines whether the regular expression \fIexp\fR matches part or
  2291. all of \fIstring\fR and returns 1 if it does, 0 if it doesn't.
  2292. See REGULAR EXPRESSIONS above for complete information on the
  2293. syntax of \fIexp\fR and how it is matched against \fIstring\fR.
  2294. .RS
  2295. .LP
  2296. If the \fB\-nocase\fR switch is specified then upper-case
  2297. characters in \fIstring\fR
  2298. are treated as lower case during the matching process.
  2299. The \fB\-nocase\fR switch must be specified before \fIexp\fR and
  2300. may not be abbreviated.
  2301. .LP
  2302. If additional arguments are specified after \fIstring\fR then they
  2303. are treated as the names of variables to use to return
  2304. information about which part(s) of \fIstring\fR matched \fIexp\fR.
  2305. \fIMatchVar\fR will be set to the range of \fIstring\fR that
  2306. matched all of \fIexp\fR.  The first \fIsubMatchVar\fR will contain
  2307. the characters in \fIstring\fR that matched the leftmost parenthesized
  2308. subexpression within \fIexp\fR, the next \fIsubMatchVar\fR will
  2309. contain the characters that matched the next parenthesized
  2310. subexpression to the right in \fIexp\fR, and so on.
  2311. .LP
  2312. Normally, \fImatchVar\fR and the \fIsubMatchVar\fRs are set to hold
  2313. the matching characters from \fBstring\fR.
  2314. However, if the \fB\-indices\fR switch is specified then each variable
  2315. will contain a list of two decimal strings giving the indices
  2316. in \fIstring\fR of the first and last characters in the matching
  2317. range of characters.
  2318. The \fB\-indices\fR switch must be specified before the \fIexp\fR
  2319. argument and may not be abbreviated.
  2320. .LP
  2321. If there are more \fIsubMatchVar\fR's than parenthesized
  2322. subexpressions within \fIexp\fR, or if a particular subexpression
  2323. in \fIexp\fR doesn't match the string (e.g. because it was in a
  2324. portion of the expression that wasn't matched), then the corresponding
  2325. \fIsubMatchVar\fR will be set to ``\fB\-1 \-1\fR'' if \fB\-indices\fR
  2326. has been specified or to an empty string otherwise.
  2327. .RE
  2328. .TP
  2329. \fBregsub \fR?\fB\-all\fR? ?\fB\-nocase\fR? \fIexp string subSpec varName\fR
  2330. This command matches the regular expression \fIexp\fR against
  2331. \fIstring\fR using the rules described in REGULAR EXPRESSIONS
  2332. above.
  2333. If there is no match, then the command returns 0 and does nothing
  2334. else.
  2335. If there is a match, then the command returns 1 and also copies
  2336. \fIstring\fR to the variable whose name is given by \fIvarName\fR.
  2337. When copying \fIstring\fR, the portion of \fIstring\fR that
  2338. matched \fIexp\fR is replaced with \fIsubSpec\fR.
  2339. If \fIsubSpec\fR contains a ``&'' or ``\e0'', then it is replaced
  2340. in the substitution with the portion of \fIstring\fR that
  2341. matched \fIexp\fR.
  2342. If \fIsubSpec\fR contains a ``\e\fIn\fR'', where \fIn\fR is a digit
  2343. between 1 and 9, then it is replaced in the substitution with
  2344. the portion of \fIstring\fR that matched the \fIn\fR-th
  2345. parenthesized subexpression of \fIexp\fR.
  2346. Additional backslashes may be used in \fIsubSpec\fR to prevent special
  2347. interpretation of ``&'' or ``\e0'' or ``\e\fIn\fR'' or
  2348. backslash.
  2349. The use of backslashes in \fIsubSpec\fR tends to interact badly
  2350. with the Tcl parser's use of backslashes, so it's generally
  2351. safest to enclose \fIsubSpec\fR in braces if it includes
  2352. backslashes.
  2353. If the \fB\-all\fR argument is specified, then all ranges in
  2354. \fIstring\fR that match \fIexp\fR are found and substitution is
  2355. performed for each of these ranges;  otherwise only the first
  2356. matching range is found and substituted.
  2357. If \fB\-all\fR is specified, then ``&'' and ``\e\fIn\fR''
  2358. sequences are handled for each substitution using the information
  2359. from the corresponding match.
  2360. If the \fB\-nocase\fR argument is specified, then upper-case
  2361. characters in \fIstring\fR are converted to lower-case before
  2362. matching against \fIexp\fR;  however, substitutions specified
  2363. by \fIsubSpec\fR use the original unconverted form of \fIstring\fR.
  2364. The \fB\-all\fR and \fB\-nocase\fR arguments must be specified
  2365. exactly:  no abbreviations are permitted.
  2366. .VE
  2367. .TP
  2368. \fBrename \fIoldName newName\fR
  2369. Rename the command that used to be called \fIoldName\fR so that it
  2370. is now called \fInewName\fR.  If \fInewName\fR is an empty string
  2371. (e.g. {}) then \fIoldName\fR is deleted.  The \fBrename\fR command
  2372. returns an empty string as result.
  2373. .TP
  2374. \fBreturn \fR?\fIvalue\fR?
  2375. Return immediately from the current procedure
  2376. (or top-level command or \fBsource\fR command),
  2377. with \fIvalue\fR as the return value.  If \fIvalue\fR is not specified,
  2378. an empty string will be returned as result.
  2379. .TP
  2380. \fBscan \fIstring format varname1 \fR?\fIvarname2 ...\fR?
  2381. This command parses fields from an input string in the same fashion
  2382. as the C \fBsscanf\fR procedure.  \fIString\fR gives the input to
  2383. be parsed and \fIformat\fR indicates how to parse it, using \fB%\fR
  2384. fields as in \fBsscanf\fR.  All of the \fBsscanf\fR options are valid;
  2385. see the \fBsscanf\fR man page for details.  Each \fIvarname\fR gives
  2386. the name of a variable; when a field is scanned from \fIstring\fR,
  2387. the result is converted back into a string and assigned to the
  2388. corresponding \fIvarname\fR.  The only unusual conversion is for
  2389. \fB%c\fR.  For \fB%c\fR conversions a single character value is
  2390. converted to a decimal string, which is then assigned to the
  2391. corresponding \fIvarname\fR;
  2392. .VS
  2393. no field width may be specified for this conversion.
  2394. .TP
  2395. \fBseek \fIfileId offset \fR?\fIorigin\fR?
  2396. Change the current access position for \fIfileId\fR.
  2397. The \fIoffset\fR and \fIorigin\fR arguments specify the position at
  2398. which the next read or write will occur for \fIfileId\fR.
  2399. \fIOffset\fR must be a number (which may be negative) and \fIorigin\fR
  2400. must be one of the following:
  2401. .RS
  2402. .TP
  2403. \fBstart\fR
  2404. The new access position will be \fIoffset\fR bytes from the start
  2405. of the file.
  2406. .TP
  2407. \fBcurrent\fR
  2408. The new access position will be \fIoffset\fR bytes from the current
  2409. access position; a negative \fIoffset\fR moves the access position
  2410. backwards in the file.
  2411. .TP
  2412. \fBend\fR
  2413. The new access position will be \fIoffset\fR bytes from the end of
  2414. the file.  A negative \fIoffset\fR places the access position before
  2415. the end-of-file, and a positive \fIoffset\fR places the access position
  2416. after the end-of-file.
  2417. .LP
  2418. The \fIorigin\fR argument defaults to \fBstart\fR.
  2419. \fIFileId\fR must have been the return
  2420. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  2421. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  2422. channels.
  2423. This command returns an empty string.
  2424. .RE
  2425. .VE
  2426. .TP
  2427. \fBset \fIvarname \fR?\fIvalue\fR?
  2428. Returns the value of variable \fIvarname\fR.
  2429. If \fIvalue\fR is specified, then set
  2430. the value of \fIvarname\fR to \fIvalue\fR, creating a new variable
  2431. if one doesn't already exist, and return its value.
  2432. .VS
  2433. If \fIvarName\fR contains an open parenthesis and ends with a
  2434. close parenthesis, then it refers to an array element:  the characters
  2435. before the open parenthesis are the name of the array, and the characters
  2436. between the parentheses are the index within the array.
  2437. Otherwise \fIvarName\fR refers to a scalar variable.
  2438. .VE
  2439. If no procedure is active, then \fIvarname\fR refers to a global
  2440. variable.
  2441. If a procedure is active, then \fIvarname\fR refers to a parameter
  2442. or local variable of the procedure, unless the \fIglobal\fR command
  2443. has been invoked to declare \fIvarname\fR to be global.
  2444. .TP
  2445. \fBsource \fIfileName\fR
  2446. Read file \fIfileName\fR and pass the contents to the Tcl interpreter
  2447. as a sequence of commands to execute in the normal fashion.  The return
  2448. value of \fBsource\fR is the return value of the last command executed
  2449. from the file.  If an error occurs in executing the contents of the
  2450. file, then the \fBsource\fR command will return that error.
  2451. If a \fBreturn\fR command is invoked from within the file, the remainder of
  2452. the file will be skipped and the \fBsource\fR command will return
  2453. normally with the result from the \fBreturn\fR command.
  2454. If \fIfileName\fR starts with a tilde, then it is tilde-substituted
  2455. as described in the \fBTcl_TildeSubst\fR manual entry.
  2456. .TP
  2457. \fBsplit \fIstring \fR?\fIsplitChars\fR?
  2458. Returns a list created by splitting \fIstring\fR at each character
  2459. that is in the \fIsplitChars\fR argument.
  2460. Each element of the result list will consist of the
  2461. characters from \fIstring\fR between instances of the
  2462. characters in \fIsplitChars\fR.
  2463. Empty list elements will be generated if \fIstring\fR contains
  2464. adjacent characters in \fIsplitChars\fR, or if the first or last
  2465. character of \fIstring\fR is in \fIsplitChars\fR.
  2466. If \fIsplitChars\fR is an empty string then each character of
  2467. \fIstring\fR becomes a separate element of the result list.
  2468. \fISplitChars\fR defaults to the standard white-space characters.
  2469. For example,
  2470. .RS
  2471. .DS
  2472. \fBsplit "comp.unix.misc" .\fR
  2473. .DE
  2474. returns \fB"comp unix misc"\fR and
  2475. .DS
  2476. \fBsplit "Hello world" {}\fR
  2477. .DE
  2478. returns \fB"H e l l o { } w o r l d"\fR.
  2479. .VE
  2480. .RE
  2481. .TP
  2482. \fBstring \fIoption arg \fR?\fIarg ...?\fR
  2483. Perform one of several string operations, depending on \fIoption\fR.
  2484. The legal \fIoption\fRs (which may be abbreviated) are:
  2485. .RS
  2486. .TP
  2487. \fBstring compare \fIstring1 string2\fR
  2488. Perform a character-by-character comparison of strings \fIstring1\fR and
  2489. \fIstring2\fR in the same way as the C \fBstrcmp\fR procedure.  Return
  2490. -1, 0, or 1, depending on whether \fIstring1\fR is lexicographically
  2491. less than, equal to, or greater than \fIstring2\fR.
  2492. .TP
  2493. \fBstring first \fIstring1 string2\fR
  2494. Search \fIstring2\fR for a sequence of characters that exactly match
  2495. the characters in \fIstring1\fR.  If found, return the index of the
  2496. first character in the first such match within \fIstring2\fR.  If not
  2497. found, return -1.
  2498. .br
  2499. .VS
  2500. .TP
  2501. \fBstring index \fIstring charIndex\fR
  2502. Returns the \fIcharIndex\fR'th character of the \fIstring\fR
  2503. argument.  A \fIcharIndex\fR of 0 corresponds to the first
  2504. character of the string.
  2505. If \fIcharIndex\fR is less than 0 or greater than
  2506. or equal to the length of the string then an empty string is
  2507. returned.
  2508. .VE
  2509. .TP
  2510. \fBstring last \fIstring1 string2\fR
  2511. Search \fIstring2\fR for a sequence of characters that exactly match
  2512. the characters in \fIstring1\fR.  If found, return the index of the
  2513. first character in the last such match within \fIstring2\fR.  If there
  2514. is no match, then return \-1.
  2515. .br
  2516. .VS
  2517. .TP
  2518. \fBstring length \fIstring\fR
  2519. Returns a decimal string giving the number of characters in \fIstring\fR.
  2520. .VE
  2521. .TP
  2522. \fBstring match \fIpattern\fR \fIstring\fR
  2523. See if \fIpattern\fR matches \fIstring\fR; return 1 if it does, 0
  2524. if it doesn't.  Matching is done in a fashion similar to that
  2525. used by the C-shell.  For the two strings to match, their contents
  2526. must be identical except that the following special sequences
  2527. may appear in \fIpattern\fR:
  2528. .RS
  2529. .IP \fB*\fR 10
  2530. Matches any sequence of characters in \fIstring\fR,
  2531. including a null string.
  2532. .IP \fB?\fR 10
  2533. Matches any single character in \fIstring\fR.
  2534. .IP \fB[\fIchars\fB]\fR 10
  2535. Matches any character in the set given by \fIchars\fR.  If a sequence
  2536. of the form
  2537. \fIx\fB\-\fIy\fR appears in \fIchars\fR, then any character
  2538. between \fIx\fR and \fIy\fR, inclusive, will match.
  2539. .IP \fB\e\fIx\fR 10
  2540. Matches the single character \fIx\fR.  This provides a way of
  2541. avoiding the special interpretation of the characters
  2542. \fB*?[]\e\fR in \fIpattern\fR.
  2543. .RE
  2544. .br
  2545. .VS
  2546. .TP
  2547. \fBstring range \fIstring first last\fR
  2548. Returns a range of consecutive characters from \fIstring\fR, starting
  2549. with the character whose index is \fIfirst\fR and ending with the
  2550. character whose index is \fIlast\fR.  An index of 0 refers to the
  2551. first character of the string.  \fILast\fR may be \fBend\fR (or any
  2552. abbreviation of it) to refer to the last character of the string.
  2553. If \fIfirst\fR is less than zero then it is treated as if it were zero, and
  2554. if \fIlast\fR is greater than or equal to the length of the string then
  2555. it is treated as if it were \fBend\fR.  If \fIfirst\fR is greater than
  2556. \fIlast\fR then an empty string is returned.
  2557. .TP
  2558. \fBstring tolower \fIstring\fR
  2559. Returns a value equal to \fIstring\fR except that all upper case
  2560. letters have been converted to lower case.
  2561. .TP
  2562. \fBstring toupper \fIstring\fR
  2563. Returns a value equal to \fIstring\fR except that all lower case
  2564. letters have been converted to upper case.
  2565. .TP
  2566. \fBstring trim \fIstring\fR ?\fIchars\fR?
  2567. Returns a value equal to \fIstring\fR except that any leading
  2568. or trailing characters from the set given by \fIchars\fR are
  2569. removed.
  2570. If \fIchars\fR is not specified then white space is removed
  2571. (spaces, tabs, newlines, and carriage returns).
  2572. .TP
  2573. \fBstring trimleft \fIstring\fR ?\fIchars\fR?
  2574. Returns a value equal to \fIstring\fR except that any
  2575. leading characters from the set given by \fIchars\fR are
  2576. removed.
  2577. If \fIchars\fR is not specified then white space is removed
  2578. (spaces, tabs, newlines, and carriage returns).
  2579. .TP
  2580. \fBstring trimright \fIstring\fR ?\fIchars\fR?
  2581. Returns a value equal to \fIstring\fR except that any
  2582. trailing characters from the set given by \fIchars\fR are
  2583. removed.
  2584. If \fIchars\fR is not specified then white space is removed
  2585. (spaces, tabs, newlines, and carriage returns).
  2586. .RE
  2587. .TP
  2588. \fBtell \fIfileId\fR
  2589. Returns a decimal string giving the current access position in
  2590. \fIfileId\fR.
  2591. \fIFileId\fR must have been the return
  2592. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  2593. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  2594. channels.
  2595. .VE
  2596. .TP
  2597. \fBtime \fIcommand\fR ?\fIcount\fR?
  2598. This command will call the Tcl interpreter \fIcount\fR
  2599. times to execute \fIcommand\fR (or once if \fIcount\fR isn't
  2600. specified).  It will then return a string of the form
  2601. .RS
  2602. .DS
  2603. \fB503 microseconds per iteration\fR
  2604. .DE
  2605. which indicates the average amount of time required per iteration,
  2606. in microseconds.
  2607. Time is measured in elapsed time, not CPU time.
  2608. .RE
  2609. .TP
  2610. \fBtrace \fIoption\fR ?\fIarg arg ...\fR?
  2611. .VS
  2612. Cause Tcl commands to be executed whenever certain operations are
  2613. invoked.  At present, only variable tracing is implemented. The
  2614. legal \fIoption\fR's (which may be abbreviated) are:
  2615. .RS
  2616. .TP
  2617. \fBtrace variable \fIname ops command\fR
  2618. Arrange for \fIcommand\fR to be executed whenever variable \fIname\fR
  2619. is accessed in one of the ways given by \fIops\fR.  \fIName\fR may
  2620. refer to a normal variable, an element of an array, or to an array
  2621. as a whole (i.e. \fIname\fR may be just the name of an array, with no
  2622. parenthesized index).  If \fIname\fR refers to a whole array, then
  2623. \fIcommand\fR is invoked whenever any element of the array is
  2624. manipulated.
  2625. .RS
  2626. .LP
  2627. \fIOps\fR indicates which operations are of interest, and consists of
  2628. one or more of the following letters:
  2629. .RS
  2630. .TP
  2631. \fBr\fR
  2632. Invoke \fIcommand\fR whenever the variable is read.
  2633. .TP
  2634. \fBw\fR
  2635. Invoke \fIcommand\fR whenever the variable is written.
  2636. .TP
  2637. \fBu\fR
  2638. Invoke \fIcommand\fR whenever the variable is unset.  Variables
  2639. can be unset explicitly with the \fBunset\fR command, or
  2640. implicitly when procedures return (all of their local variables
  2641. are unset).  Variables are also unset when interpreters are
  2642. deleted, but traces will not be invoked because there is no
  2643. interpreter in which to execute them.
  2644. .RE
  2645. .LP
  2646. When the trace triggers, three arguments are appended to
  2647. \fIcommand\fR so that the actual command is as follows:
  2648. .DS C
  2649. \fIcommand name1 name2 op\fR
  2650. .DE
  2651. \fIName1\fR and \fIname2\fR give the name(s) for the variable
  2652. being accessed:  if the variable is a scalar then \fIname1\fR
  2653. gives the variable's name and \fIname2\fR is an empty string;
  2654. if the variable is an array element then \fIname1\fR gives the
  2655. name of the array and name2 gives the index into the array;
  2656. if an entire array is being deleted and the trace was registered
  2657. on the overall array, rather than a single element, then \fIname1\fR
  2658. gives the array name and \fIname2\fR is an empty string.
  2659. \fIOp\fR indicates what operation is being performed on the
  2660. variable, and is one of \fBr\fR, \fBw\fR, or \fBu\fR as
  2661. defined above.
  2662. .LP
  2663. \fICommand\fR executes in the same context as the code that invoked
  2664. the traced operation:  if the variable was accessed as part of a
  2665. Tcl procedure, then \fIcommand\fR will have access to the same
  2666. local variables as code in the procedure.  This context may be
  2667. different than the context in which the trace was created.
  2668. If \fIcommand\fR invokes a procedure (which it normally does) then
  2669. the procedure will have to use \fBupvar\fR or \fBuplevel\fR if it
  2670. wishes to access the traced variable.
  2671. Note also that \fIname1\fR may not necessarily be the same as the name
  2672. used to set the trace on the variable;  differences can occur if
  2673. the access is made through a variable defined with the \fBupvar\fR
  2674. command.
  2675. .LP
  2676. For read and write traces, \fIcommand\fR can modify
  2677. the variable to affect the result of the traced operation.
  2678. If \fIcommand\fR modifies the value of a variable during a
  2679. read or write trace, then the new value will be returned as the
  2680. result of the traced operation.
  2681. The return value from  \fIcommand\fR is ignored except that
  2682. if it returns an error of any sort then the traced operation
  2683. is aborted with an error message saying that the access was denied
  2684. (this mechanism can be used to implement read-only variables, for
  2685. example).
  2686. For write traces, \fIcommand\fR is invoked after the variable's
  2687. value has been changed; it can write a new value into the variable
  2688. to override the original value specified in the write operation.
  2689. To implement read-only variables, \fIcommand\fR will have to restore
  2690. the old value of the variable.
  2691. .LP
  2692. While \fIcommand\fR is executing during a read or write trace, traces
  2693. on the variable are temporarily disabled.
  2694. This means that reads and writes invoked by
  2695. \fIcommand\fR will occur directly, without invoking \fIcommand\fR
  2696. (or any other traces) again.
  2697. .LP
  2698. When an unset trace is invoked, the variable has already been
  2699. deleted:  it will appear to be undefined with no traces.
  2700. If an unset occurs because of a procedure return, then the
  2701. trace will be invoked in the variable context of the procedure
  2702. being returned to:  the stack frame of the returning procedure
  2703. will no longer exist.
  2704. Traces are not disabled during unset traces, so if an unset trace
  2705. command creates a new trace and accesses the variable, the
  2706. trace will be invoked.
  2707. .LP
  2708. If there are multiple traces on a variable they are invoked
  2709. in order of creation, most-recent first.
  2710. If one trace returns an error, then no further traces are
  2711. invoked for the variable.
  2712. If an array element has a trace set, and there is also a trace
  2713. set on the array as a whole, the trace on the overall array
  2714. is invoked before the one on the element.
  2715. .LP
  2716. Once created, the trace remains in effect either until the
  2717. trace is removed with the \fBtrace vdelete\fR command described
  2718. below, until the variable is unset, or until the interpreter
  2719. is deleted.
  2720. Unsetting an element of array will remove any traces on that
  2721. element, but will not remove traces on the overall array.
  2722. .LP
  2723. This command returns an empty string.
  2724. .RE
  2725. .TP
  2726. \fBtrace vdelete \fIname ops command\fR
  2727. If there is a trace set on variable \fIname\fR with the
  2728. operations and command given by \fIops\fR and \fIcommand\fR,
  2729. then the trace is removed, so that \fIcommand\fR will never
  2730. again be invoked.
  2731. Returns an empty string.
  2732. .TP
  2733. \fBtrace vinfo \fIname\fR
  2734. Returns a list containing one element for each trace
  2735. currently set on variable \fIname\fR.
  2736. Each element of the list is itself a list containing two
  2737. elements, which are the \fIops\fR and \fIcommand\fR associated
  2738. with the trace.
  2739. If \fIname\fR doesn't exist or doesn't have any traces set, then
  2740. the result of the command will be an empty string.
  2741. .RE
  2742. .TP
  2743. \fBunknown \fIcmdName \fR?\fIarg arg ...\fR?
  2744. This command doesn't actually exist as part of Tcl, but Tcl will
  2745. invoke it if it does exist.
  2746. If the Tcl interpreter encounters a command name for which there
  2747. is not a defined command, then Tcl checks for the existence of
  2748. a command named \fBunknown\fR.
  2749. If there is no such command, then the interpeter returns an
  2750. error.
  2751. If the \fBunknown\fR command exists, then it is invoked with
  2752. arguments consisting of the fully-substituted name and arguments
  2753. for the original non-existent command.
  2754. The \fBunknown\fR command typically does things like searching
  2755. through library directories for a command procedure with the name
  2756. \fIcmdName\fR, or expanding abbreviated command names to full-length,
  2757. or automatically executing unknown commands as UNIX sub-processes.
  2758. In some cases (such as expanding abbreviations) \fBunknown\fR will
  2759. change the original command slightly and then (re-)execute it.
  2760. The result of the \fBunknown\fR command is used as the result for
  2761. the original non-existent command.
  2762. .TP
  2763. \fBunset \fIname \fR?\fIname name ...\fR?
  2764. Remove one or more variables.
  2765. Each \fIname\fR is a variable name, specified in any of the
  2766. ways acceptable to the \fBset\fR command.
  2767. If a \fIname\fR refers to an element of an array, then that
  2768. element is removed without affecting the rest of the array.
  2769. If a \fIname\fR consists of an array name with no parenthesized
  2770. index, then the entire array is deleted.
  2771. The \fBunset\fR command returns an empty string as result.
  2772. An error occurs if any of the variables doesn't exist.
  2773. .VE
  2774. .TP
  2775. \fBuplevel \fR?\fIlevel\fR?\fI command \fR?\fIcommand ...\fR?
  2776. All of the \fIcommand\fR arguments are concatenated as if they had
  2777. been passed to \fBconcat\fR; the result is then evaluated in the
  2778. variable context indicated by \fIlevel\fR.  \fBUplevel\fR returns
  2779. the result of that evaluation.  If \fIlevel\fR is an integer, then
  2780. it gives a distance (up the procedure calling stack) to move before
  2781. executing the command.  If \fIlevel\fR consists of \fB#\fR followed by
  2782. a number then the number gives an absolute level number.  If \fIlevel\fR
  2783. is omitted then it defaults to \fB1\fR.  \fILevel\fR cannot be
  2784. defaulted if the first \fIcommand\fR argument starts with a digit or \fB#\fR.
  2785. For example, suppose that procedure \fBa\fR was invoked
  2786. from top-level, and that it called \fBb\fR, and that \fBb\fR called \fBc\fR.
  2787. Suppose that \fBc\fR invokes the \fBuplevel\fR command.  If \fIlevel\fR
  2788. is \fB1\fR or \fB#2\fR  or omitted, then the command will be executed
  2789. in the variable context of \fBb\fR.  If \fIlevel\fR is \fB2\fR or \fB#1\fR
  2790. then the command will be executed in the variable context of \fBa\fR.
  2791. If \fIlevel\fR is \fB3\fR or \fB#0\fR then the command will be executed
  2792. at top-level (only global variables will be visible).
  2793. The \fBuplevel\fR command causes the invoking procedure to disappear
  2794. from the procedure calling stack while the command is being executed.
  2795. In the above example, suppose \fBc\fR invokes the command
  2796. .RS
  2797. .DS
  2798. \fBuplevel 1 {set x 43; d}
  2799. .DE
  2800. where \fBd\fR is another Tcl procedure.  The \fBset\fR command will
  2801. modify the variable \fBx\fR in \fBb\fR's context, and \fBd\fR will execute
  2802. at level 3, as if called from \fBb\fR.  If it in turn executes
  2803. the command
  2804. .DS
  2805. \fBuplevel {set x 42}
  2806. .DE
  2807. then the \fBset\fR command will modify the same variable \fBx\fR in \fBb\fR's
  2808. context:  the procedure \fBc\fR does not appear to be on the call stack
  2809. when \fBd\fR is executing.  The command ``\fBinfo level\fR'' may
  2810. be used to obtain the level of the current procedure.
  2811. \fBUplevel\fR makes it possible to implement new control
  2812. constructs as Tcl procedures (for example, \fBuplevel\fR could
  2813. be used to implement the \fBwhile\fR construct as a Tcl procedure).
  2814. .RE
  2815. .TP
  2816. \fBupvar \fR?\fIlevel\fR? \fIotherVar myVar \fR?\fIotherVar myVar \fR...?
  2817. .VS
  2818. This command arranges for one or more local variables in the current
  2819. procedure to refer to variables in an enclosing procedure call or
  2820. to global variables.
  2821. \fILevel\fR may have any of the forms permitted for the \fBuplevel\fR
  2822. command, and may be omitted if the first letter of the first \fIotherVar\fR
  2823. isn't \fB#\fR or a digit (it defaults to \fB1\fR).
  2824. For each \fIotherVar\fR argument, \fBupvar\fR makes the variable
  2825. by that name in the procedure frame given by \fIlevel\fR (or at
  2826. global level, if \fIlevel\fR is \fB#0\fR) accessible
  2827. in the current procedure by the name given in the corresponding
  2828. \fImyVar\fR argument.
  2829. The variable named by \fIotherVar\fR need not exist at the time of the
  2830. call;  it will be created the first time \fImyVar\fR is referenced, just like
  2831. an ordinary variable.
  2832. \fBUpvar\fR may only be invoked from within procedures.
  2833. Neither \fIotherVar\fR or \fImyVar\fR may refer to an element of an
  2834. array.
  2835. \fBUpvar\fR returns an empty string.
  2836. .RS
  2837. .LP
  2838. The \fBupvar\fR command simplifies the implementation of call-by-name
  2839. procedure calling and also makes it easier to build new control constructs
  2840. as Tcl procedures.
  2841. For example, consider the following procedure:
  2842. .DS
  2843. .ta 1c 2c 3c
  2844. \fBproc add2 name {
  2845.     upvar $name x
  2846.     set x [expr $x+2]
  2847. }
  2848. .DE
  2849. \fBAdd2\fR is invoked with an argument giving the name of a variable,
  2850. and it adds two to the value of that variable.
  2851. Although \fBadd2\fR could have been implemented using \fBuplevel\fR
  2852. instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fBadd2\fR
  2853. to access the variable in the caller's procedure frame.
  2854. .VE
  2855. .RE
  2856. .TP
  2857. \fBwhile \fItest body
  2858. .VS
  2859. The \fIwhile\fR command evaluates \fItest\fR as an expression
  2860. (in the same way that \fBexpr\fR evaluates its argument).
  2861. The value of the expression must be numeric; if it is non-zero
  2862. then \fIbody\fR is executed by passing it to the Tcl interpreter.
  2863. Once \fIbody\fR has been executed then \fItest\fR is evaluated
  2864. again, and the process repeats until eventually \fItest\fR
  2865. evaluates to a zero numeric value.  \fBContinue\fR
  2866. commands may be executed inside \fIbody\fR to terminate the current
  2867. iteration of the loop, and \fBbreak\fR
  2868. commands may be executed inside \fIbody\fR to cause immediate
  2869. termination of the \fBwhile\fR command.  The \fBwhile\fR command
  2870. always returns an empty string.
  2871. .VE
  2872.  
  2873. .SH "BUILT-IN VARIABLES"
  2874. .PP
  2875. The following global variables are created and managed automatically
  2876. by the Tcl library.  Except where noted below, these variables should
  2877. normally be treated as read-only by application-specific code and by users.
  2878. .TP
  2879. \fBenv\fR
  2880. .br
  2881. .VS
  2882. This variable is maintained by Tcl as an array
  2883. whose elements are the environment variables for the process.
  2884. Reading an element will return the value of the corresponding
  2885. environment variable.
  2886. Setting an element of the array will modify the corresponding
  2887. environment variable or create a new one if it doesn't already
  2888. exist.
  2889. Unsetting an element of \fBenv\fR will remove the corresponding
  2890. environment variable.
  2891. Changes to the \fBenv\fR array will affect the environment
  2892. passed to children by commands like \fBexec\fR.
  2893. If the entire \fBenv\fR array is unset then Tcl will stop
  2894. monitoring \fBenv\fR accesses and will not update environment
  2895. variables.
  2896. .TP
  2897. \fBerrorCode\fR
  2898. After an error has occurred, this variable will be set to hold
  2899. additional information about the error in a form that is easy
  2900. to process with programs.
  2901. \fBerrorCode\fR consists of a Tcl list with one or more elements.
  2902. The first element of the list identifies a general class of
  2903. errors, and determines the format of the rest of the list.
  2904. The following formats for \fBerrorCode\fR are used by the
  2905. Tcl core; individual applications may define additional formats.
  2906. .RS
  2907. .TP
  2908. \fBCHILDKILLED\fI pid sigName msg\fR
  2909. This format is used when a child process has been killed because of
  2910. a signal.  The second element of \fBerrorCode\fR will be the
  2911. process's identifier (in decimal).
  2912. The third element will be the symbolic name of the signal that caused
  2913. the process to terminate; it will be one of the names from the
  2914. include file signal.h, such as \fBSIGPIPE\fR.
  2915. The fourth element will be a short human-readable message
  2916. describing the signal, such as ``write on pipe with no readers''
  2917. for \fBSIGPIPE\fR.
  2918. .TP
  2919. \fBCHILDSTATUS\fI pid code\fR
  2920. This format is used when a child process has exited with a non-zero
  2921. exit status.  The second element of \fBerrorCode\fR will be the
  2922. process's identifier (in decimal) and the third element will be the exit
  2923. code returned by the process (also in decimal).
  2924. .TP
  2925. \fBCHILDSUSP\fI pid code\fR
  2926. This format is used when a child process has been suspended because
  2927. of a signal.
  2928. The second element of \fBerrorCode\fR will be the process's identifier,
  2929. in decimal.
  2930. The third element will be the symbolic name of the signal that caused
  2931. the process to suspend; this will be one of the names from the
  2932. include file signal.h, such as \fBSIGTTIN\fR.
  2933. The fourth element will be a short human-readable message
  2934. describing the signal, such as ``background tty read''
  2935. for \fBSIGTTIN\fR.
  2936. .TP
  2937. \fBNONE\fR
  2938. .br
  2939. This format is used for errors where no additional information is
  2940. available for an error besides the message returned with the
  2941. error.  In these cases \fBerrorCode\fR will consist of a list
  2942. containing a single element whose contents are \fBNONE\fR.
  2943. .TP
  2944. \fBUNIX \fIerrName msg\fR
  2945. If the first element of \fBerrorCode\fR is \fBUNIX\fR, then
  2946. the error occurred during a UNIX kernel call.
  2947. The second element of the list will contain the symbolic name
  2948. of the error that occurred, such as \fBENOENT\fR; this will
  2949. be one of the values defined in the include file errno.h.
  2950. The third element of the list will be a human-readable
  2951. message corresponding to \fIerrName\fR, such as
  2952. ``no such file or directory'' for the \fBENOENT\fR case.
  2953. .PP
  2954. To set \fBerrorCode\fR, applications should use library
  2955. procedures such as \fBTcl_SetErrorCode\fR and
  2956. \fBTcl_UnixError\fR, or they may invoke the \fBerror\fR command.
  2957. If one of these methods hasn't been used, then the Tcl
  2958. interpreter will reset the variable to \fBNONE\fR after
  2959. the next error.
  2960. .RE
  2961. .VE
  2962. .TP
  2963. \fBerrorInfo\fR
  2964. After an error has occurred, this string will contain one or more lines
  2965. identifying the Tcl commands and procedures that were being executed
  2966. when the most recent error occurred.
  2967. Its contents take the form of a stack trace showing the various
  2968. nested Tcl commands that had been invoked at the time of the error.
  2969.  
  2970. .SH AUTHOR
  2971. John Ousterhout, University of California at Berkeley (ouster@sprite.berkeley.edu)
  2972. .sp
  2973. Many people have contributed to Tcl in various ways, but the following
  2974. people have made unusually large contributions:
  2975. .sp
  2976. .nf
  2977. Bill Carpenter
  2978. Peter Da Silva
  2979. Mark Diekhans
  2980. Karl Lehenbauer
  2981. Mary Ann May-Pumphrey
  2982.